• 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
Re: KVO can be unsafe in -init?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: KVO can be unsafe in -init?


  • Subject: Re: KVO can be unsafe in -init?
  • From: Ken Ferry <email@hidden>
  • Date: Tue, 8 Sep 2009 20:02:29 -0700

Hi John,
Since you're passing NSKeyValueObservingOptionInitial,
-observeValueForKeyPath: is going to be invoked on your object immediately,
before the method even returns.

Is your -observeValueForKeyPath: safe to be called with a partially set up
object?

Do you have any subclasses of your object?  If so, -observeValueForKeyPath:
is getting called on them before they've done their work in init.

-Ken

On Mon, Sep 7, 2009 at 7:36 AM, John Chang <email@hidden> wrote:

> Hi all,
> Question: is it unsafe for some reason to be adding yourself as a KVO
> observer during -init?
>
> We have a singleton with an -init that looks something like this:
> - (id)init
> {
>
> if ((self = [super init]))
> {
>
> _foo = [[NSMutableDictionary alloc] init];
> _bar = [[NSMutableDictionary alloc] init];
>
> [[XYZManager sharedManager] addObserver:self forKeyPath:@"allObjects"
> options:NSKeyValueObservingOptionInitial context:NULL];
>
> }
> return self;
>
> }
> This code is running on iPhone OS. On some devices (we haven't been to
> narrow this down), the last line of code is throwing an exception:
>
> Sun Sep  6 13:41:26 unknown MyApp[1609] <Error>: *** Terminating app due to
> uncaught exception 'NSInvalidArgumentException', reason: '***
> -[NSCFDictionary setObject:forKey:]: attempt to insert nil key'
>
>
> We know that [XYZManager sharedManager] can't be nil, since otherwise
> -addObserver: would not be getting called, and the first line "if ((self =
> [super init]))" ensures that self can't be nil. We're pretty sure
> that [XYZManager sharedManager].allObjects is not nil, but even if it were
> nil, that shouldn't cause an exception. -allObjects is a synthesized getter
> with no dependent keys. No other threads are involved here.
>
> So we're stumped as to why KVO is throwing an exception. The only theory is
> that the NSKeyValueObservingOptionInitial option is causing KVO to do
> something with self immediately, but self isn't fully "set up" yet. Though
> it's unclear why in this particular usage that would be unsafe, or why it
> would make any difference. Can anyone provide any insight?
>
> Thanks,
> /John
> Stockholm, Sweden
> _______________________________________________
>
> 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
>
_______________________________________________

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

References: 
 >KVO can be unsafe in -init? (From: John Chang <email@hidden>)

  • Prev by Date: Re: [iPhone] : How to customize Phonepad ?
  • Next by Date: Re: KVO can be unsafe in -init?
  • Previous by thread: Re: KVO can be unsafe in -init?
  • Next by thread: Re: KVO can be unsafe in -init?
  • Index(es):
    • Date
    • Thread