简体   繁体   中英

NSOperation - main method runs on the main thread?

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;
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM