Preliminary study of iOS multi thread (six): NSOperation

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

The iOS platform provides with more advanced (asynchronous) interface, so you can concentrate energy to the design tasks to be completed code, to avoid the thread generation to write has nothing to do with the program logic, operation management code. Of course, is essentially the interface implies generation of thread and thread management operation, thus more concise implementation of multithread. Use the following first to study NSOperation and NSOperationQueue classes.

The essence of NSOperation is encapsulated to concurrently running code, some of the main interface and NSThread is basically the same, can be seen as not running ability of thread thread abstract class A. Refer to the NSThread, some of the same interface with NSOperation:

- (void)start; //In the current task status and dependency suitable case, start the task main method of NSOperation, the need to pay attention to the default implementation is only in the current thread running. If you need to execute concurrently, the subclass must override this method, and the   - (BOOL) isConcurrent method returns YES

- (void)main; //The main task of the definition of NSOperation code

- (BOOL)isCancelled; //The current task status is marked as removed

- (void)cancel; //Cancel the current NSOperation task, is the essence of labeled isCancelled state

- (BOOL)isExecuting; //The NSOperation task is running

- (BOOL)isFinished; //The NSOperation task is complete

NSOperation other commonly used methods, including dependence:

- (BOOL)isReady; //Is ready to run, the value and the relations of tasks related to

- (void)addDependency:(NSOperation *)op; //Dependence and task, i.e. dependent tasks are completed, in order to perform the current task

- (void)removeDependency:(NSOperation *)op; //Dependent cancel task, task dependent relations will not be automatically eliminated, must call this method

- (NSArray *)dependencies; //Get all the dependent NSOperation task

And for task synchronization:

- (void)waitUntilFinished; //The current thread is blocked, until the end of NSOperation. Synchronization can be used for thread execution order

- (void)setCompletionBlock:(void (^)(void))block; //Run the setup NSOperation after the end of the block code, the NSOperation may be cancelled, the core task so this block operation code should be independent and NSOperation.

In addition to the succession of NSOperation to realize the concurrent code, usually more simple way is to use its two sub class of NSInvocationOperation or NSBlockOperation, and then added to the NSOperationQueue queue to run. Part of the sample code is as follows:

NSInvocationOperation *opA = [[NSInvocationOperationalloc] initWithTarget:self selector:@selector(operate) object:nil];

NSBlockOperation *opB = [NSBlockOperation blockOperationWithBlock:^{

[self operate];


- (void)operate


  //thread loop

  while (condition)





NSOperationQueue *queue = [[NSOperationQueuealloc] init];

queue.maxConcurrentOperationCount = 2; //Set the maximum number of concurrent execution, if 1 is also only a concurrent task in the operation, can control the order of execution

[queue addOperation:opA]; //Added to the execution queue, if isReady is executed

[queue addOperation:opB]; //Ibid, need to pay attention to the opA and opB in concurrent operation

[queue waitUntilAllOperationsAreFinished]; //The current thread to wait, until opA and opB are the end of execution

If the opB in the opA after the completion of the execution needs began, with dependencies can be:

[opB addDependency:opA];

You can also use a synchronization method for waitUntilFinished, join in the previous example:

NSBlockOperation *opB = [NSBlockOperation blockOperationWithBlock:^{

   [opA waitUntilFinished]; //The opB thread to wait until the end of the opA implementation (normal termination or cancellation)

[self operate];


Establish dependencies or wait for the relationship, must avoid circular dependencies or to wait for the cycle, otherwise it will cause a thread deadlock.

Finally, have a look the other common NSOperationQueue methods:

- (void)addOperations:(NSArray *)ops waitUntilFinished:(BOOL)wait; //Batch adding execute operation, wait mark whether the current thread to wait for all the operation after the end, return

- (void)addOperationWithBlock:(void (^)(void))block; //The equivalent of adding a NSBlockOperation to perform a task

- (NSArray *)operations; //Returns the join execution arrays in operation, when a operation after the end will automatically removed from the array

- (NSUInteger)operationCount //Returns the number of operation has joined the Executive

- (void)setSuspended:(BOOL)b; //If the suspension will be the implementation of the operation, but does not pause has begun to operation

- (BOOL)isSuspended; //Return to suspend mark

- (void)cancelAllOperations; //Cancel all the implementation of operation, is the essence of the call to the cancel method of the operation

+ (id)currentQueue; //Returns the current NSOperationQueue, if the current thread is not running in the NSOperationQueue returns nil

+ (id)mainQueue; //Returns the NSOperationQueue of the main thread, the default is a queue

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Griffith at November 16, 2013 - 11:50 PM