Ben Copsey

Start using a custom runloopmode to stop the iphone trying to draw from secondary threads

Who knows - this might even be more efficient!
@@ -123,6 +123,7 @@ @@ -123,6 +123,7 @@
123 123
124 NSTimeInterval timeOutSeconds; 124 NSTimeInterval timeOutSeconds;
125 125
  126 + NSAutoreleasePool *pool;
126 } 127 }
127 128
128 #pragma mark init / dealloc 129 #pragma mark init / dealloc
@@ -13,6 +13,8 @@ @@ -13,6 +13,8 @@
13 #import "ASIHTTPRequest.h" 13 #import "ASIHTTPRequest.h"
14 #import "NSHTTPCookieAdditions.h" 14 #import "NSHTTPCookieAdditions.h"
15 15
  16 +static CFStringRef ASIHTTPRequestRunMode = CFSTR("ASIHTTPRequest");
  17 +
16 static NSString *NetworkRequestErrorDomain = @"com.Your-Company.Your-Product.NetworkError."; 18 static NSString *NetworkRequestErrorDomain = @"com.Your-Company.Your-Product.NetworkError.";
17 19
18 static const CFOptionFlags kNetworkEvents = kCFStreamEventOpenCompleted | 20 static const CFOptionFlags kNetworkEvents = kCFStreamEventOpenCompleted |
@@ -136,6 +138,10 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -136,6 +138,10 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
136 // Create the request 138 // Create the request
137 - (void)main 139 - (void)main
138 { 140 {
  141 +
  142 + [pool release];
  143 + pool = [[NSAutoreleasePool alloc] init];
  144 +
139 complete = NO; 145 complete = NO;
140 146
141 // Create a new HTTP request. 147 // Create a new HTTP request.
@@ -199,6 +205,7 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -199,6 +205,7 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
199 // Start the request 205 // Start the request
200 - (void)loadRequest 206 - (void)loadRequest
201 { 207 {
  208 + CFRunLoopAddCommonMode(CFRunLoopGetCurrent(),ASIHTTPRequestRunMode);
202 209
203 [authenticationLock release]; 210 [authenticationLock release];
204 authenticationLock = [[NSConditionLock alloc] initWithCondition:1]; 211 authenticationLock = [[NSConditionLock alloc] initWithCondition:1];
@@ -234,12 +241,12 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -234,12 +241,12 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
234 } 241 }
235 242
236 // Schedule the stream 243 // Schedule the stream
237 - CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); 244 + CFReadStreamScheduleWithRunLoop(readStream, CFRunLoopGetCurrent(), ASIHTTPRequestRunMode);
238 245
239 // Start the HTTP connection 246 // Start the HTTP connection
240 if (!CFReadStreamOpen(readStream)) { 247 if (!CFReadStreamOpen(readStream)) {
241 CFReadStreamSetClient(readStream, 0, NULL, NULL); 248 CFReadStreamSetClient(readStream, 0, NULL, NULL);
242 - CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); 249 + CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), ASIHTTPRequestRunMode);
243 CFRelease(readStream); 250 CFRelease(readStream);
244 readStream = NULL; 251 readStream = NULL;
245 [self failWithProblem:@"Unable to start http connection"]; 252 [self failWithProblem:@"Unable to start http connection"];
@@ -276,8 +283,12 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -276,8 +283,12 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
276 break; 283 break;
277 } 284 }
278 [self updateProgressIndicators]; 285 [self updateProgressIndicators];
279 - [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:endDate]; 286 + CFRunLoopRunInMode(ASIHTTPRequestRunMode,0.5,YES);
  287 + //[[NSRunLoop currentRunLoop] runMode:NSRunLoopCommonModes beforeDate:endDate];
280 } 288 }
  289 +
  290 + [pool release];
  291 + pool = nil;
281 } 292 }
282 293
283 // Cancel loading and clean up 294 // Cancel loading and clean up
@@ -286,7 +297,7 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -286,7 +297,7 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
286 if (readStream) { 297 if (readStream) {
287 CFReadStreamClose(readStream); 298 CFReadStreamClose(readStream);
288 CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL); 299 CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);
289 - CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); 300 + CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), ASIHTTPRequestRunMode);
290 CFRelease(readStream); 301 CFRelease(readStream);
291 readStream = NULL; 302 readStream = NULL;
292 } 303 }
@@ -811,7 +822,7 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy @@ -811,7 +822,7 @@ static void ReadStreamClientCallBack(CFReadStreamRef readStream, CFStreamEventTy
811 if (readStream) { 822 if (readStream) {
812 CFReadStreamClose(readStream); 823 CFReadStreamClose(readStream);
813 CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL); 824 CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);
814 - CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); 825 + CFReadStreamUnscheduleFromRunLoop(readStream, CFRunLoopGetCurrent(), ASIHTTPRequestRunMode);
815 CFRelease(readStream); 826 CFRelease(readStream);
816 readStream = NULL; 827 readStream = NULL;
817 } 828 }
This diff is collapsed. Click to expand it.
This diff could not be displayed because it is too large.