iOS - How to know when NSOperationQueue finish processing a few operations?

前端 未结 4 738
我寻月下人不归
我寻月下人不归 2020-12-04 23:01

I need in my application to download directories and their content. So I decided to implement a NSOperationQueue and I subclassed NSOperation to implement NSURLRequest etc..

4条回答
  •  醉话见心
    2020-12-04 23:27

    Add a "Done" NSOperation which has all other NSOperations for one directory as dependency.

    Something like this:

    NSInvocationOperation *doneOp = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(done:) object:nil];
    
    NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
    [queue addOperation:op1];
    [doneOp addDependency:op1];
    
    NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
    [queue addOperation:op2];
    [doneOp addDependency:op2];
    
    NSInvocationOperation *op3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(doSomething:) object:nil];
    [queue addOperation:op3];
    [doneOp addDependency:op3];
    
    [queue addOperation:doneOp];
    

    doneOp will only run after op1, op2 and op3 have finished executing.

提交回复
热议问题