Erik Chen

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.
@@ -3507,7 +3507,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -3507,7 +3507,7 @@ static NSOperationQueue *sharedQueue = nil;
3507 3507
3508 // If request has asked delegate or ASIAuthenticationDialog for credentials 3508 // If request has asked delegate or ASIAuthenticationDialog for credentials
3509 } else if ([self authenticationNeeded]) { 3509 } else if ([self authenticationNeeded]) {
3510 - CFRunLoopStop(CFRunLoopGetCurrent()); 3510 + // Do nothing.
3511 } 3511 }
3512 3512
3513 } 3513 }
@@ -3547,8 +3547,6 @@ static NSOperationQueue *sharedQueue = nil; @@ -3547,8 +3547,6 @@ static NSOperationQueue *sharedQueue = nil;
3547 if (!wasFinished) 3547 if (!wasFinished)
3548 [self didChangeValueForKey:@"isFinished"]; 3548 [self didChangeValueForKey:@"isFinished"];
3549 3549
3550 - CFRunLoopStop(CFRunLoopGetCurrent());  
3551 -  
3552 #if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0 3550 #if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
3553 if ([ASIHTTPRequest isMultitaskingSupported] && [self shouldContinueWhenAppEntersBackground]) { 3551 if ([ASIHTTPRequest isMultitaskingSupported] && [self shouldContinueWhenAppEntersBackground]) {
3554 dispatch_async(dispatch_get_main_queue(), ^{ 3552 dispatch_async(dispatch_get_main_queue(), ^{
@@ -4787,7 +4785,7 @@ static NSOperationQueue *sharedQueue = nil; @@ -4787,7 +4785,7 @@ static NSOperationQueue *sharedQueue = nil;
4787 BOOL runAlways = YES; // Introduced to cheat Static Analyzer 4785 BOOL runAlways = YES; // Introduced to cheat Static Analyzer
4788 while (runAlways) { 4786 while (runAlways) {
4789 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 4787 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
4790 - CFRunLoopRun(); 4788 + CFRunLoopRunInMode(kCFRunLoopDefaultMode, 1.0e10, true);
4791 [pool drain]; 4789 [pool drain];
4792 } 4790 }
4793 4791