对象不从NSOperationQueue删除执行
问题描述:
在我的NSOperation的子类后,我设置4个标志,并且当操作完成它的执行不会被删除到的NSOperation队列,它是在开始时加入,这件事情的原因在我的应用程序中有很多问题。 我想,我设置这些标志的方式是不正确的,请你帮忙吧。因为我真的花了很多时间来确定这个问题。对象不从NSOperationQueue删除执行
@property(assign, nonatomic) BOOL isCancelled;
@property(nonatomic, getter=isExecuting) BOOL executing;
@property(nonatomic, getter=isFinished) BOOL finished;
@property(readonly, getter=isAsynchronous) BOOL asynchronous;
//in initialisation
- (id)initWithURL:(NSURL*)url andRaw:(NSInteger)row
{
if (![super init])
return nil;
[self setTargetURL:url];
return self;
}
//the way I override KVO
- (BOOL)isExecuting
{
NSLog(@"Exec");
return (self.defaultSession != nil);//it doesn't work
}
- (BOOL)isFinished
{
NSLog(@"Finished");
return (self.defaultSession == nil); //it doesn't work, so I explicitly set the value
}
- (BOOL)isAsynchronous
{
return YES;
}
- (void)cancel
{
[super cancel];
[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
self.isExecuting = NO;
self.isFinished = YES;
[self didChangeValueForKey:@"isFinished"];
[self didChangeValueForKey:@"isExecuting"];
if(self.downloadTask.state == NSURLSessionTaskStateRunning)
[self.downloadTask cancel];
[self finish];
}
- (void)finish
{
[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
self.defaultSession = nil; //NSURLSession
self.isFinished = YES;
[self didChangeValueForKey:@"isFinished"];
[self didChangeValueForKey:@"isExecuting"];
}
预先感谢您
编辑: 终于让我找到了问题 - 这是NSURLSession队列中。它保持对队列的强烈引用,并且不允许将它从NSOperationQueue中解除分配和删除。
答
我也做了同样的事情,在斯威夫特albiet。
有几个方面,我已经实现方式不同,如下表所列:)
- 我已经注意到,我们没有覆盖取消(在 异步操作方法。 NSOperation取消 方法的默认行为是将self.cancelled布尔值设置为true。
- self.executing应该只内操作的被覆盖的start()方法来设置为true,但不是在init(不知道这港岛线造成任何问题)。在start()方法中设置self.executing = true之前,我确保布尔self.cancelled为false。如果self.cancelled = true,我们应该设置self.finished = true。
- 另外,我还创建了isExecuting和isFinished性质,我叫willChangeValueForKey和didChangeValueForKey一个“didSet”属性的观察者。我不是100%确定如何复制中的OBJ C的didSet行为(This说要overrride二传手)
请参考有关“并发”雷Wenderlich视频教程山姆·戴维斯解释的NSOperation子类的创建用于异步操作。请注意,它仅适用于订阅者,并在Swift中进行了解释。我相信如果你解决了第1点和第2点,你应该看到你的问题得到解决。
谢谢,我会尽量按照你的指示,不幸的是我不是Ray Wenderlich的用户(。 – Melany
我强烈建议成为一个,如果可以的话,他们有一些很棒的教程。 .raywenderlich.com /视频教程 – shrutim
OK也许我会,如果管理这个问题) – Melany