Ios 在主菜单中取消了NSO操作访问 现状
我已经创建了一个自定义的Ios 在主菜单中取消了NSO操作访问 现状,ios,objective-c,xcode,nsoperation,Ios,Objective C,Xcode,Nsoperation,我已经创建了一个自定义的NSOperation对象,我想在取消时更新一些数据 我遵循了中的说明,但我没有覆盖cancel方法 这是我的标题: // MyOperation.h @interface MyOperation : NSOperation { } @property (nonatomic, retain) OtherDataClass *dataClass; @end 以及实施情况 // MyOperation.m @implementation MyOperation @synt
NSOperation
对象,我想在取消时更新一些数据
我遵循了中的说明,但我没有覆盖cancel
方法
这是我的标题:
// MyOperation.h
@interface MyOperation : NSOperation {
}
@property (nonatomic, retain) OtherDataClass *dataClass;
@end
以及实施情况
// MyOperation.m
@implementation MyOperation
@synthesize dataClass;
- (void)main {
if ([self isCancelled]) {
[self.dataClass setStatusCanceled];
NSLog(@"Operation cancelled");
}
// Do some work here
NSLog(@"Working... working....")
[self.dataClass setStatusFinished];
NSLog(@"Operation finished");
}
@end
问题
我在一个队列中有几个操作。我希望,当我在队列中调用cancelAllOperations
时,我会在日志中得到“Operation cancelled”文本,并在我的另一个类中更新状态,但它不起作用。队列中的操作未调用main
方法
为什么会发生这种情况?我该如何解决
笔记
我试图用以下内容覆盖取消方法:
- (void)cancel {
[super cancel];
[self.dataClass setStatusCanceled];
NSLog(@"Operation cancelled");
}
它正在工作,但我已经了解到不应重写此方法。当您调用cancelAllOperations
时,已启动的操作将被取消设置为YES
。尚未启动的操作不会启动。我不明白为什么会再次调用main
?i、 e您启动NSOperationmain
并执行其操作。调用cancelAllOperations
不会再次调用main
,因为这将再次开始“工作”?我在文档中没有看到任何说明您不应覆盖取消。你能给我一个推荐人吗?我不想打main
不止一次。例如,我在一个队列中有5个操作。如果我在计算第三个操作时调用cancelAllOperations
,我希望获得前两个操作的“操作已完成”和当前操作(完成时),然后是最后两个操作的“操作已取消”文本。但是我没有得到最后两行日志,因为一旦队列被取消,main
似乎不会为最后两行调用。关于重写cancel
方法,我在几个StackOverflow答案中看到了它。