I am using NSOperation subclass in my app which will do following 4 tasks in a single operation, i wanted all these 4 tasks to run on background thread so I wrapped up into single NSOperation class, so that I can easily either pause or cancel it
Tasks
- long time running calculation
- fetching data from core data
- Updating to server
- Updating coredata
here each has to execute synchronously which means each one is depend on another one except long time running calculation.
Code
// MyOperation.h
@interface MyOperation : NSOperation {
}
@property (nonatomic, getter = isCancelled) BOOL cancelled;
@end
// MyOperation.m
@implementation MyOperation
- (void)cancel
{
   @synchronized (self)
   {
     if (!self.cancelled)
     {
        [self willChangeValueForKey:@"isCancelled"];
        self.cancelled = YES;
        [webServiceOperation cancel]
        [self didChangeValueForKey:@"isCancelled"];
     }
   }
}
- (void)main {
   if ([self isCancelled]) {
    NSLog(@"** operation cancelled **");
    return;
   }    
   @autoreleasepool{
    [self performTasks];
   }
}
- (void)performTasks {
    [self calculate:^{
          if (self.isCancelled)
              return;
          [self fetchDataFromCoredata:^{
                if (self.isCancelled)
                    return;
                //I am using AFNetWorking for updateWebService task so it shall run on separate NSOperation so it would be like nested NSOPeration
                webServiceOperation = [self updateWebService:^{
                                            if (self.isCancelled)
                                            return;
                                            [self updateCoreData:^{
                                                  if (self.isCancelled)
                                                        return;
                                            }];
             }];
        }];
    }];
}
@end
I am assuming that I am not following proper approach because when I tested this code using KVO the NSOperationQueuegets complete notification before it reaches calculate's completion block.
Questions
- Is my approach right?
- If not, can you some please guide me the right approach?
- If it is right approach then why does NSOPerationQueueget complete notification before completion block execution?
Thanks in advance! looking forward your response
 
     
     
     
     
    