Reputation: 77646
Why does my NSOperation subclass run the main method on the main thread? I want it to run asynchronously.
@interface ServiceClient : NSOperation
@end
@implementation ServiceClient
- (void)main
{
if ([[NSThread currentThread] isEqual:[NSThread mainThread]])
{
NSLog(@"Why does this get called on the main thread");
}
}
@end
Starting the operation
ServiceClient *serviceClient = [[ServiceClient aloc] init];
[serviceClient start];
EDIT:
Documentation suggest overriding isCuncurrent and returning yes, but the method does not get called.
- (BOOL)isConcurrent
{
return YES;
}
Upvotes: 0
Views: 2691
Reputation: 38728
You need to set up your own thread in the start
method
Snippet taken from Concurrency Programming Guide
start
(Required) All concurrent operations must override this method and replace the default behavior with their own custom implementation. To execute an operation manually, you call its start method. Therefore, your implementation of this method is the starting point for your operation and is where you set up the thread or other execution environment in which to execute your task. Your implementation must not call super at any time.
If you add your NSOperation
to an NSOperationQueue
it takes care of this for you.
Upvotes: 3