Asynchronous methods in NSOperation

后端 未结 3 2015
挽巷
挽巷 2020-12-08 00:55

I\'m fetching some data from Facebook Connect (using the FBConnect Objective-C 2.0 framework) and I\'m doing all that in an NSOperation. It is in an NSOperation because I ha

3条回答
  •  -上瘾入骨i
    2020-12-08 01:08

    Below is a full example. In your subclass, after your async method completes, call [self completeOperation] to transition to the finished state.

    @interface AsynchronousOperation()
    // 'executing' and 'finished' exist in NSOperation, but are readonly
    @property (atomic, assign) BOOL _executing;
    @property (atomic, assign) BOOL _finished;
    @end
    
    @implementation AsynchronousOperation
    
    - (void) start;
    {
        if ([self isCancelled])
        {
            // Move the operation to the finished state if it is canceled.
            [self willChangeValueForKey:@"isFinished"];
            self._finished = YES;
            [self didChangeValueForKey:@"isFinished"];
            return;
        }
    
        // If the operation is not canceled, begin executing the task.
        [self willChangeValueForKey:@"isExecuting"];
        [NSThread detachNewThreadSelector:@selector(main) toTarget:self withObject:nil];
        self._executing = YES;
        [self didChangeValueForKey:@"isExecuting"];
    
    }
    
    - (void) main;
    {
        if ([self isCancelled]) {
            return;
        }
    
    }
    
    - (BOOL) isAsynchronous;
    {
        return YES;
    }
    
    - (BOOL)isExecuting {
        return self._executing;
    }
    
    - (BOOL)isFinished {
        return self._finished;
    }
    
    - (void)completeOperation {
        [self willChangeValueForKey:@"isFinished"];
        [self willChangeValueForKey:@"isExecuting"];
    
        self._executing = NO;
        self._finished = YES;
    
        [self didChangeValueForKey:@"isExecuting"];
        [self didChangeValueForKey:@"isFinished"];
    }
    
    @end
    

提交回复
热议问题