gpt4 book ai didi

ios - NSOperation 访问 isCancelled in main

转载 作者:行者123 更新时间:2023-11-29 10:36:35 27 4
gpt4 key购买 nike

现状

我已经创建了一个自定义的 NSOperation 对象,我想在它被取消时更新一些数据。

我已按照 this answer 中的说明进行操作而且我没有覆盖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” 文本,并在我的其他类(class)中更新状态,但它不管用。队列中的操作未调用 main 方法。

为什么会发生这种情况,我该如何解决?

注意事项

我试图用这个覆盖 cancel 方法:

- (void)cancel {
[super cancel];
[self.dataClass setStatusCanceled];
NSLog(@"Operation cancelled");
}

它正在工作,但我读到不应覆盖此方法。

最佳答案

当您调用cancelAllOperations 时,已经开始的操作会将isCancelled 设置为YES。尚未开始的操作将不会开始。

关于ios - NSOperation 访问 isCancelled in main,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26627324/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com