Friday, 30 October 2015

NSOperationQueue and NSOperation

GCD is a lightweight way to represent units of work that are going to be executed concurrently. You don’t schedule these units of work; the system takes care of scheduling for you. Adding dependency among blocks can be a headache. Canceling or suspending a block creates extra work for you as a developer! .

NSOperationQueue and NSOperation add a little extra overhead compared to GCD, but you can add dependency among various operations. You can re-use operations, cancel or suspend them. NSOperation is compatible with Key-Value Observation(KVO) .

NSOperationQueue regulates the execution of a set of NSOperation objects . After being added to queue an operation remains in the queue until it is explicitly  canceled or finishes it  executing its task .
We can add inter operation dependencies among operations  which provide absolute execution order for operations even if  those operation located in different operations queues .
An operation object is not considered ready to execute until all of its dependent operation have finished executing .
Operations are always executed on a separate thread, regardless of  whether they are designated as asynchronous or synchronous .

NSOperation is an abstract class we use to encapsulate the code and data associated with a single task . We subclass it or use one of the system defined subclasses (NSInvocationOperation or NSBlockOperation) to perform actual task.
An operation executes its task once and can't be used to execute again .

To create a customize operation , follow these steps : 
1) Subclass NSOperation .
2) Override main .
3) Create an autoreleasepool in  main .
4) Put your code in autoreleasepool .

The reason we should create our own autorelease pool is that we do not have access to the autorelease pool of the main thread, so we should create your own.

example:
#import <Foundation/Foundation.h>

@interface MyOperation: Operation
@end

@implementation MyOperation

-(void)main{
      @autoreleasepool{
       //define task here
       NSLog(@"custom operation");
      }
}
}
@end

NSOperationQueue API:
1) To get a queue use:
   NSOperationQueue *myQueue = [NSOperationQueue alloc]init];
   myQueue.name = @"queueName";

2) Maximum number of concurrent operations: To set maximum number of operations that  NSOperationQueue can run concurrently .
   myQueue.MaxConcurrentOperationCount = 2;

3) To add operation to queue :
   [myQueue  addOperation:customOperation] ;

4) To pause a queue :
  [myQueue setSuspended:YES];

   To resume , set the setSuspended to NO .
5) To cancel operation in a queue:
   [myQueue cancelAllOperations];

NSOperation API:

1) To make an operation dependent on other operation:

   lets assume op1 and op2 are two custom operation object .
 [op2 addDependency op1];

2) To set the priority of the operation :

[op1 setQueuePriority:NSOperationQueuePriorityVeryLow];

3) To set completionBlock :

[op1 setCompletionBlock: ^{
   NSLog(@"operation has completed).
}];
 

No comments:

Post a Comment