I have various kinds of operations (derived from NSOperation) to do async queries over the Internet. As is the norm, I determine when they're finished by observing their isFinished property.
When one particular type of operation finishes, I want to create a follow-up type of operation using info from the first. But when I do so, calling addObserver on the operation crashes my app with a bad access. Since observers are called in a random thread, I tried creating the follow-up operation and setting the observer on the main thread. No difference. This is being done in observeValueForKeyPath:
GetMessagesOperation* msgOp =
[[GetMessagesOperation alloc] initWithUserID:_user.getID()
sinceLast:true
includeSystem:true
includeUser:false
skipRows:0
maxCount:50
DBManager:_pDatabaseMgr];
[msgOp addObserver:self
forKeyPath:@"isFinished"
options:0
context:getMessageContext];
[_operationQueue addOperation:msgOp];
[msgOp release];
The context is a void* to a string; I use the same syntax for many other operations that work fine. Here's how the contexts are defined:
static void* systemInfoContext = (void*)@"sys";
static void* validateUserContext = (void*)@"user";
static void* getMessageContext = (void*)@"msg";
Anybody have a guess here? Thanks!
Edit: Thanks for the reply. There's no crash log generated. It just quits with a bad access on the addObserver line, and this happens regardless of whether I execute in on the main thread or the background thread in which observeValueForKeyPath is entered.
BAH! The whole problem was a failure to call [super init] in the operation's initialization method.
Thanks to all who answered anyway.