iOS停止全局队列运行

时间:2015-06-03 06:34:07

标签: ios xcode grand-central-dispatch

代码:

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秒调用一个方法。它运行正常。问题是全局队列也在其他控制器中继续运行。如何阻止此运行?任何帮助将不胜感激。< / p>

1 个答案:

答案 0 :(得分:2)

您可以保留BOOL属性,在每次通话之前,您都要检查此属性。如果要停止,请将其设置为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];
                       }
                   });


               });
}