为什么我的NSOperation子类从未完成? [英] Why is my NSOperation subclass never finishing?
本文介绍了为什么我的NSOperation子类从未完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 NSOperation
子类,我想同时运行。
I have an NSOperation
subclass that I want to run concurrently.
我的理解是,工作:
- 我需要定义
isConcurrent
才能返回c> li>我需要发送
isExecuting
和isFinished
的KVO通知。 -
isExecuting
和@synthesize
会自动传送适当的KVO通知> isFinished 已更改。
- I need to define
isConcurrent
to returnYES
. - I need to define the
start
method - I need to send KVOs notification for
isExecuting
andisFinished
when it's done. - Using
@synthesize
will automatically send the appropriate KVO notifications when the values forisExecuting
andisFinished
are changed.
尽管如此,我已验证我的队列不会移动到下一个
Despite this, I have verified that my queue never moves on to the next item.
这里是我的代码的肉:
@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
我缺少什么?
(这是在iPhone上, )
(This is on an iPhone, but I can't imagine that matters.)
推荐答案
它看起来像什么KVO通知 @synthesize
发送不足以 NSOperationQueue
继续。
It looks like whatever KVO notifications @synthesize
sends aren't enough for NSOperationQueue
to move on.
手动发送通知修复问题:
Sending the notifications manually fixes the problem:
- (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];
}
}
另请参阅:
- Why does NSOperation disable automatic key-value observing?
这篇关于为什么我的NSOperation子类从未完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文