I have app that performs login to web service and then requests list of objects using sessionid received during Login.
Appdelegate.h
....
@property (nonatomic, retain) NSString *sessionId;
AppDelegate.m:
-(id)init{
queue = [[NSOperationQueue alloc] init];
[queue setMaxConcurrentOperationCount:1];
.....
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
LoginOperation *loginOperation = [[LoginOperation alloc] init];
[queue addOperation:loginOperation];
[loginOperation release];
ListOperation *listOperation = [[ListOperation alloc] init];
[queue addOperation:listOperation];
[listOperation release];
}
LoginOperation:
-(void) main {
...
[[UIApplication sharedApplication] delegate] setSessionId:sessionID];
...
}
ListOperation:
-(void)main{
//Crashes at next line:
NSString *sessionId = [[UIApplication sharedApplication] delegate] sessionId] ;
}
It crash if I acces s any proeprty in any singleton object or AppDelegate. Debugger shows that singleton object or Appdelegate is valid and initialized but ANY property of that object is invalid and access leads to crash.
This is some strange threading-related gotcha. The only thing I can think of is that NSOperation has invalid copies of all other objects in it's thread or something like that.
It doesn't crash if doing same in manually spawned thread using [NSThread detachNewThreadSelector:@selector(performList) toTarget:self withObject:nil]; I want to use NSOperation instead of NSThread detach... because NSOperation provides queue.
What is the optimal pattern for such situation? defining ListOperation as Concurrent Operation? I don't want complicated mess with defining Concurrent Operation.
I think my case is fairly simple and there should be simple solution?