Subclassificação de NSOperation para ser simultânea e cancelável
Não consigo encontrar boa documentação sobre como subclassificarNSOperation
para ser simultâneo e também para apoiar o cancelamento. Eu li os documentos da Apple, mas não consigo encontrar um exemplo "oficial".
Aqui está o meu código fonte:
@synthesize isExecuting = _isExecuting;
@synthesize isFinished = _isFinished;
@synthesize isCancelled = _isCancelled;
- (BOOL)isConcurrent
{
return YES;
}
- (void)start
{
/* WHY SHOULD I PUT THIS ?
if (![NSThread isMainThread])
{
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
return;
}
*/
[self willChangeValueForKey:@"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:@"isExecuting"];
if (_isCancelled == YES)
{
NSLog(@"** OPERATION CANCELED **");
}
else
{
NSLog(@"Operation started.");
sleep(1);
[self finish];
}
}
- (void)finish
{
NSLog(@"operationfinished.");
[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
_isExecuting = NO;
_isFinished = YES;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
if (_isCancelled == YES)
{
NSLog(@"** OPERATION CANCELED **");
}
}
No exemplo que encontrei, não entendo por que performSelectorOnMainThread: é usado. Impediria que minha operação fosse executada simultaneamente.
Além disso, quando eu comento essa linha, executo minhas operações simultaneamente. No entanto, oisCancelled
flag não é modificado, mesmo que eu tenha chamadocancelAllOperations
.