问题
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