developerWab
developerWab
采纳率69.1%
2013-07-09 08:25 阅读 2.7k
已采纳

终止异步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)
            }
        }
});

能否终止或取消此线程?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    redCoral_ redCoral_ 2013-07-09 08:59

    使用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];  
    }
    
    点赞 评论 复制链接分享

相关推荐