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 (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com