• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Dynamically subclassing an observed object
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Dynamically subclassing an observed object


  • Subject: Dynamically subclassing an observed object
  • From: Dave DeLong <email@hidden>
  • Date: Wed, 13 Oct 2010 13:44:14 -0600

Hi everyone,

I maintain some code that does dynamic subclassing to override an object's -dealloc method to do some extra cleanup prior to deallocation.  (And for the curious, this cleanup is not necessary when using Garbage Collection)

However, I observed a problem this morning, and that's when I try to dynamically subclass an object that has some observers attached.  Something is going on inside the KVO mechanism that apparently can't handle the class of the object not being the NSKVONotifying_* class.  (Or perhaps it can and I just haven't overridden the necessary stuff)

I tried adding an overridden -class method to my dynamic subclass which would simply return [super class] (since NSKVONotifying_* classes appear to be hiding their class as well), but that also did not work.

I considered doing something like inserting my subclass as the superclass of the NSKVONotifying_* class, but the documentation for class_setSuperclass() says "You should not use this function.", so I figured that I should probably not use this approach.

The problem would manifest itself when I had an object observing one of its own synthesized properties.  If that object had been dynamically subclass by my code *after* registering itself as an observer, then I would starting getting unrecognized selector crashes from inside the _NSSetObjectAndNotify function of the KVO'd setter.  These crashes were usually something like an unrecognized selector of "+[NSCFString objectForKey:]".

If I made sure that my subclassing happened before adding observers, then things would work properly.

However, trying to enforce that subclassing happens before observation registration would require some significant refactoring of a dependent code base, which I don't have time for (we've entered our internal beta testing period).  So currently what I've done is added a custom -dealloc method to the existing class (whether it's an NSKVONotifying_* class or not), then exchange that dealloc method with the original one.  The end result of this is that when the object receives the -dealloc message, it invokes the new method, which then forwards on to the original dealloc method, which should then forward things on to super.

This, however, seems a bit more fragile than the dynamic subclassing approach, and curiosity has gotten the better of me.  So my question is:

What's involved with safely subclassing an object that's already been dynamically subclassed with KVO?

Thanks,

Dave

Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Dynamically subclassing an observed object
      • From: Ken Thomases <email@hidden>
    • Re: Dynamically subclassing an observed object
      • From: Kyle Sluder <email@hidden>
  • Prev by Date: Re: remove icon from Dock
  • Next by Date: Re: Dynamically subclassing an observed object
  • Previous by thread: [ANN] CocoaHeads-NYC tonight, Wed 10/13
  • Next by thread: Re: Dynamically subclassing an observed object
  • Index(es):
    • Date
    • Thread