Why does my NSOperation subclass never end?

I have a subclass NSOperationthat I want to run at the same time.

I understand that for simultaneous operations:

  • I need to determine isConcurrentfor a refund YES.
  • I need to define a method start
  • I need to send a KVO notice for isExecutingand isFinishedwhen it will be done.
  • Usage @synthesizewill automatically send appropriate KVO notifications when the values ​​for isExecutingand are changed isFinished.

Despite this, I confirmed that my turn never moves on to the next element.

Here is the meat of my code:

@interface MyOperation()

@property (readwrite) BOOL isExecuting;
@property (readwrite) BOOL isFinished;

@end

@implementation MyOperation

- (void)start
{
    @autoreleasepool {
        self.isExecuting = YES;
        self.HTTPOperation = [[AFHTTPRequestOperation alloc] initWithRequest: URLRequest];

        _HTTPOperation.completionBlock = [^{
            [self completed];

            self.isExecuting = NO;
            self.isFinished = YES;
        } copy];

        [_HTTPOperation start];
    }
}

- (BOOL)isConcurrent
{
    return YES;
}

- (void)completed
{
}

@end

What am I missing?

(This is on the iPhone, but I can’t imagine that it matters.)

+3
2

, KVO @synthesize NSOperationQueue.

:

- (void)start
{
    @autoreleasepool {
        [self willChangeValueForKey:@"isExecuting"];
        self.isExecuting = YES;
        [self didChangeValueForKey:@"isExecuting"];

        NSURLRequest *URLRequest = [self buildRequest];
        if (!URLRequest) {
            [self willChangeValueForKey:@"isFinished"];
            [self willChangeValueForKey:@"isExecuting"];
            _isExecuting = NO;
            _isFinished = YES;
            [self didChangeValueForKey:@"isExecuting"];
            [self didChangeValueForKey:@"isFinished"];
            return;
        }

        self.HTTPOperation = [[AFHTTPRequestOperation alloc] initWithRequest: URLRequest];

        _HTTPOperation.completionBlock = [^{
            [self completed];

            [self willChangeValueForKey:@"isFinished"];
            [self willChangeValueForKey:@"isExecuting"];
            _isExecuting = NO;
            _isFinished = YES;
            [self didChangeValueForKey:@"isExecuting"];
            [self didChangeValueForKey:@"isFinished"];
        } copy];

        [_HTTPOperation start];
    }
}

. :

+4

""? NSOperationQueue?

, , : P

NSOperation KVO .

, , NSOperation :

@interface MyOperation : NSOperation

@property (assign) id<MyOperationDelegate> delegate;

@synthesize delegate;
@synthesize error;

    -(id)init{
    self = [super init];
    if(self){
        [self addObserver:self forKeyPath:@"isFinished" 
                  options:NSKeyValueObservingOptionNew 
                  context:NULL];
    }
    return self;
}

-(void)dealloc{
    [self removeObserver:self forKeyPath:@"isFinished"];
    [super dealloc];
}

-(void)observeValueForKeyPath:(NSString *)keyPath 
                     ofObject:(id)object change:(NSDictionary *)change context:(void *)context{

    if([keyPath isEqualToString:@"isFinished"] == YES){
    if([self isCancelled] == NO){
        if(delegate != nil && [delegate respondsToSelector:@selector(operationComplete:)]){
            [delegate taskComplete:self];
        }
    }else{
        if(delegate != nil && [delegate respondsToSelector:@selector(operationCancelled)]){
            [delegate taskCancelled];
        }
    }
}

}

-(void)main{
    [NSException exceptionWithName:kTaskException 
                            reason:@"Only to be used with subclass" 
                          userInfo:nil];
}

, ,

@class MyOperation;
@protocol MyOperationDelegate <NSObject>

@optional
-(void)operationComplete:(MyOperation*)operation;
-(void)operationCancelled;
+1

All Articles