Stop an NSRunLoop

2020-03-13 08:57发布

问题:

I have a connection in a thread, so I add it to the run loop to get all data:

  [[NSRunLoop currentRunLoop] run];
  [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

But I can't find any way to stop it

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    if([NSRunLoop currentRunLoop]){
        [[NSRunLoop currentRunLoop] cancelPerformSelectorsWithTarget:self];
    }
    [connection cancel];
}

How can I stop this loop?

回答1:

You can stop the runloop by Core Fundation API :

CFRunLoopStop(CFRunLoopGetCurrent());