OGeek|极客世界-中国程序员成长平台

标题: ios - viewWillDisappear 时 NSOperationQueue cancelAllOperations 不起作用 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:09
标题: ios - viewWillDisappear 时 NSOperationQueue cancelAllOperations 不起作用

我的代码:

NSOperationQueue *queue;

-(void)viewDidLoad
{
    queue = [NSOperationQueue new];
    NSOperation* loadImgOp = [[NSInvocationOperation alloc] initWithTarget:self selectorselector(refresh) object:nil];
    [queue addOperation:loadImgOp];

}

-(void)refresh
{
   [self operationFirst];         
   [self operationSecond];
   ... 
   [self operationFive];
   dispatch_async(dispatch_get_main_queue(), ^{
      [self.tableView reloadData];
   });

}

- (void)viewWillDisappearBOOL)animated {
   [super viewWillDisappear:animated];
   [queue cancelAllOperations];     
}

当我调用 new ViewController 时,-(void)refresh 会继续工作。 cancelAllOperations 不起作用。



Best Answer-推荐答案


您不能在当前配置中。您应该添加一个 cancelled 属性并在整个方法中检查它。

在队列上调用取消将操作标记为已取消并阻止新操作运行,但它不会终止正在运行的操作。由每个操作来管理其取消。在 then are 的调用中它不能做任何事情,因为它已经调用了目标方法。

关于ios - viewWillDisappear 时 NSOperationQueue cancelAllOperations 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28178342/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4