开发者社区> 问答> 正文

关于终止异步DISPATCH_QUEUE的问题

每次开启一个DISPATCH_QUEUE需要三四分钟。我需要一个按钮可以终止队列。怎么实现?

dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        @synchronized(self)
        {
            for (int i = 0; i < (4000); i++) {
                         (Some methods)
            }
        }
});

能否终止或取消此线程?

展开
收起
爵霸 2016-03-19 09:14:36 2061 0
1 条回答
写回答
取消 提交回答
  • 使用dispatch_sync阻塞了主线程

    你应该用 NSOperationQueue

    NSOperationQueue *_myQueue; //instance var
    
    _myQueue = [[NSOperationQueue alloc] init]; //init it
    
    _myQueue.suspended = (buttonPressed) ? YES : NO; //toggle it like you need
    
    for (int i = 0; i < (4000); i++) {
       [_queue addOperationWithInvocation:NSInvocation for method to call];  
    }
    2019-07-17 19:07:11
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载