From 80e1dc8b6559bea7aea0d2bdcf831d51f6661e76 Mon Sep 17 00:00:00 2001 From: Erik Chen Date: Mon, 17 Oct 2011 21:08:29 -0700 Subject: [PATCH] Clean up the use of CFRunLoop. Previously, tasks running from inside the CFRunLoop would call CFRunLoopStop prior to finishing execution. This results in non-deterministic failures to call endBackgroundTask when running iOS tasks in the background with beginBackgroundTaskWithExpirationHandler. Since the only reason to break out of the CFRunLoop is to flush the autorelease pool, we instead invoke CFRunLoopRunInMode with returnAfterSourceHandled=True, which safely returns and flushes the autorelease pool after each invocation of the CFRunLoop. --- Classes/ASIHTTPRequest.m | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Classes/ASIHTTPRequest.m b/Classes/ASIHTTPRequest.m index 8b239edc..377f9b7b 100644 --- a/Classes/ASIHTTPRequest.m +++ b/Classes/ASIHTTPRequest.m @@ -3507,7 +3507,7 @@ - (void)handleStreamComplete // If request has asked delegate or ASIAuthenticationDialog for credentials } else if ([self authenticationNeeded]) { - CFRunLoopStop(CFRunLoopGetCurrent()); + // Do nothing. } } @@ -3547,8 +3547,6 @@ - (void)markAsFinished if (!wasFinished) [self didChangeValueForKey:@"isFinished"]; - CFRunLoopStop(CFRunLoopGetCurrent()); - #if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 if ([ASIHTTPRequest isMultitaskingSupported] && [self shouldContinueWhenAppEntersBackground]) { dispatch_async(dispatch_get_main_queue(), ^{ @@ -4787,7 +4785,7 @@ + (void)runRequests BOOL runAlways = YES; // Introduced to cheat Static Analyzer while (runAlways) { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - CFRunLoopRun(); + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, true); [pool drain]; }