문제

I've got an NSOperation queue, and four NSOperations which run in it.

NSOperationQueue myQueue = [[NSOperationQueue alloc] init];
NSOperation readOperation = [[NSOperation alloc] init];
NSOperation postOperation = [[NSOperation alloc] init];
NSOperation deleteOperation = [[NSOperation alloc] init];

I'm aware a cancel can be called an NSOperation object. If I call a

[postOperation cancel];

does it get cancelled immediately from myQueue?

Also I would like to cancel the deleteOperation from the postOperation.

Does this work?

postOperation = [NSBlockOperation blockOperationWithBlock: ^{
                                [deleteOperation cancel];
                                /**** do a HTTP post ****/
                                }];
[myQueue addOperation:postOperation];

Essentially I want to cancel a delete operation before I do the POST, if if that operation was executing. Also does

[myQueue setMaxConcurrentOperationCount:1];

ensure that the operation queue is FIFO?

도움이 되었습니까?

해결책

Per NSOperation documentation:

... if an operation is in a queue but waiting on unfinished dependent operations, those operations are subsequently ignored. ... allows the operation queue to call the operation’s start method sooner and clear the object out of the queue.

the queue will call the operation's start methods immediately which should then mark it as finished without doing any useful work.

Note that you could override this method is subclasses. Apple asks you to create the same behavior as in NSOperation, but it's still up to the developer.

does [myQueue setMaxConcurrentOperationCount:1]; ensure that the operation queue is FIFO?

That's a separate question. The answer is no. You don't have control over order of operations other then setting dependencies (which is what you should be doing).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top