iOS stop global queue from running

旧城冷巷雨未停 提交于 2019-12-08 05:16:13

问题


Code:

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];

        });

      });

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.


回答1:


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;

Function

-(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];
                       }
                   });


               });
}


来源:https://stackoverflow.com/questions/30612503/ios-stop-global-queue-from-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!