1

Maybe I let NSOperation to play a wrong role in non-concurrent job. My requirement is , I want to do a lot of async jobs, but I want them to be completed in order. When task1 is finished after the async callback, task2 can be take into work now. And I make all the task a NSOperation. However, NSOperation is used to multiple thread programming most time. Is my choice wrong. But it remind me to think more about the NSOperation in this case, we can't manage manually the isFinished and isExecute in a sync block since the operation have been release in non-concurrent nsoperation,it means i couldnot use the powerful operation queue to automatically manage the task.Any idea?Thanks for your answer..

edit with code :

-(void)main {
    [super main];
    self.isOperationExcuting = YES;
    self.isOperationFinished = NO;
    WEAKSELF
    [self query:^(NSArray *array, NSError *error) {
        //I set my custom property, but it do not cause my NSOperation to be finished
        weakSelf.isOperationFinished = YES;
        weakSelf.isOperationExcuting = NO;
    }];
}
-(void)query:(void (^)(NSArray *array, NSError *error))block {
    BmobQuery *query = [BmobQuery queryWithClassName:@"Room"];
    [query findObjectsInBackgroundWithBlock:block];
}
-(BOOL)isFinished {
    return self.isOperationFinished;
}
- (BOOL)isExecuting {
    return self.isOperationExcuting;
}
- (void)start {
    [super start];
    NSLog(@"start");
}
- (void)cancel {
    [super cancel];
    NSLog(@"cancel");
}
seguedestination
  • 419
  • 4
  • 19
  • Think what your looking for is a `NSOperationQueue`, with `maxConcurrentOperationCount` set to one. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSOperationQueue_class/ – BooRanger Nov 09 '15 at 09:30

1 Answers1

0

Just make all added operations serial by setting maxConcurrentOperationCount to 1.

NSOperationQueue* queue = [[ NSOperationQueue alloc ] init];
queue.maxConcurrentOperationCount = 1;

[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:operation3];

NSOperationQueue

The NSOperationQueue class regulates the execution of a set of NSOperation objects. After being added to a queue, an operation remains in that queue until it is explicitly canceled or finishes executing its task.

Rafał Sroka
  • 39,540
  • 23
  • 113
  • 143
  • My aim is : `[operation1 completionBlock:^(){ [operation2 completionBlock:^(){ [operation3 completionBlock:^(){ }]; }]`; }]; – seguedestination Nov 09 '15 at 10:36
  • Is there any reason why you need to chain operations in their completion handlers? – Rafał Sroka Nov 09 '15 at 10:38
  • yeah... I have to start a operation in the previous operation's completion handlers.. thus I can do the job serially. is the NSOperation improper? – seguedestination Nov 09 '15 at 10:41
  • `NSOperation` is proper, but instead of starting operations in completions handlers create a `NSOperationQueue` and add your operations there. When you set the `maxConcurrentOperationCount` to `1` your operations will be run serially one after another as you want it. – Rafał Sroka Nov 09 '15 at 10:45
  • I think I know what you mean, use queue can call the operation to start..But my operations have influence each other, In operation1' completion handler , it will return a BOOL value that is needed to operation2.For example, I want to pause the current song, then play a new song. If I take to pause operation and play operation into the queue, I cannot control them , and there exist one situation that my pause operation may failed, but it will play two song together..I need to know the pause operation is successful that I excute play operation.. – seguedestination Nov 09 '15 at 10:52
  • 1
    Alright, now I get it. You can achieve this with creating dependancies between operations. There is `addDependency:` method of `NSOperation`. I would also subclass `NSOperation` and add a dictionary property for passing data between operations. Check out this question http://stackoverflow.com/questions/17402283/pass-data-from-nsoperation-to-next-nsoperation – Rafał Sroka Nov 09 '15 at 11:03
  • thanks for your understand.. Now a new problem, dependency is good choice, but operaion2 will be excute when operation1's 'finished = YES', however If I use custom property, I have no idea to let operation1 finished when the async task in main function is finished. I have posted my code.. – seguedestination Nov 09 '15 at 11:08
  • Sroka thanks for your patient answer, I have found the way, I can use kvo or notification in the async completion handler to finish operation1, and operation2 depend on operation1, thus they are excuted one by one! – seguedestination Nov 09 '15 at 11:56