iOS停止运行全局队列 [英] iOS stop global queue from running

查看:90
本文介绍了iOS停止运行全局队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

dispatch_async(
      dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        [ServerAPI API_GetChatList:self
                        withUserId:[self getUserIDFromUserDefaults]
                    withScheduleId:strGroupId
                          withType:@"group"];

        dispatch_async(dispatch_get_main_queue(), ^{
          [self performSelector:@selector(getChatList)
                     withObject:nil
                     afterDelay:10];

        });

      });

我使用调度全局队列每10秒调用一个方法,它工作正常.问题是全局队列也在其他控制器中继续运行.如何停止运行该队列?将不胜感激.

I used dispatch global queue to call a method for every 10 seconds.It is working fine.The problem is global queue is keep running in other controllers too.How do i stop this from running?any help will be appreciated.

推荐答案

您可以保留BOOL属性,在每次调用之前,请检查该属性.要停止时,将其设置为YES

You can keep a BOOL property,before every call, you check this property. When you want to stop, set it to YES

@property (atomic) BOOL stop;

功能

-(void)getChatList {
   if (self.stop) {
        return;
   }

dispatch_async(
               dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                   //Do something
                   dispatch_async(dispatch_get_main_queue(), ^{
                       if (!self.stop) {

                           [self performSelector:@selector(getChatList)
                                      withObject:nil
                                      afterDelay:10];
                       }
                   });


               });
}

这篇关于iOS停止运行全局队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆