dependency management - NSOperationQueue: a sequence of NSOperation's with dependencies VS (maxConcurrentOperationCount == 1)? -
dependency management - NSOperationQueue: a sequence of NSOperation's with dependencies VS (maxConcurrentOperationCount == 1)? -
for illustration have 3 objects:
nsoperation *op1 = ...; nsoperation *op2 = ...; nsoperation *op3 = ...; [op3 adddependency:op2]; [op2 adddependency:op1]; nsoperationqueue *queue = ...; queue.maxconcurrentoperationcount = 1; [queue addoperations:@[op1, op2, op3] waituntilfinished:no];
i add together operations in right order. illustration if op2
cancelled should cancel op3
, can't clear queue in case.
my questions:
1)is safe combine such sequences of operations maxconcurrentoperationcount == 1
?
2)what programme if swap around op1
, op2
? (op2
should performed after op1
queue able take 1 operations simultaneously)
p.s. in application utilize afhttprequestoperation
. inheritance hierarchy:
afhttprequestoperation
-> afurlconnectionoperation
-> nsoperation
so can't take other subclass of nsoperation
.
to reply questions:
it's safe combine particular sequence of operations dependencies have givenmaxconcurrentoperations = 1
. the queue run op2
, op3
, op1
or op2
, op1
, op3
if reverse dependency order of op1
, op2
. theres nil tricky in dependency chain you've specified , nsoperationqueue
can take care of things automatically. can problem if specify circular dependency (e.g op3
depends on op1
), or have operation isn't added queue, , can't executed satisfy dependency.
apple has cancellation in the nsoperationqueue class reference:
canceling operation causes operation ignore dependencies may have. behavior makes possible queue execute operation’s start method possible. start method, in turn, moves operation finished state can removed queue.
all nsoperation subclasses should handle cancellation correctly first checking see if has been cancelled , finish operation without performing actions. if isn't done it's bug, , operations may execute though have been cancelled.
(interestingly applies nsblockoperation, didn't realise. explicitly need check self.iscancelled
in block).
i used coderunner on app store seek out , modified programme slightly. it's reproduced below.
#import <foundation/foundation.h> int main(int argc, char *argv[]) { @autoreleasepool { nsoperation *op1 = [nsblockoperation blockoperationwithblock:^{ nslog(@"op1"); }]; nsoperation *op2 = [nsblockoperation blockoperationwithblock:^{ nslog(@"op2"); }]; nsoperation *op3 = [nsblockoperation blockoperationwithblock:^{ nslog(@"op3"); }]; [op3 adddependency:op2]; [op2 adddependency:op1]; nsoperationqueue *queue = [[nsoperationqueue alloc] init]; queue.maxconcurrentoperationcount = 1; [queue addoperations:@[op1, op2, op3] waituntilfinished:yes]; } }
for nsblockoperation
refer need this, bit disgusting looks improve in nsoperation
subclass can refer self
.
__block nsblockoperation *op1 = [nsblockoperation blockoperationwithblock:^{ nslog(@"op1 cancelled=%d", op1.cancelled); }];
dependencies dependency-management nsoperation nsoperationqueue afhttprequestoperation
Comments
Post a Comment