From ed36cba63c928900be3c9424c4ee24b158cdaa06 Mon Sep 17 00:00:00 2001 From: Alexander Kress Date: Fri, 4 Feb 2011 21:38:53 -0500 Subject: [PATCH] Updated SeriouslyOperation to allow it to run on separate thread --- src/SeriouslyOperation.m | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/src/SeriouslyOperation.m b/src/SeriouslyOperation.m index b7df522..fa67233 100644 --- a/src/SeriouslyOperation.m +++ b/src/SeriouslyOperation.m @@ -61,19 +61,33 @@ - (id)initWithRequest:(NSURLRequest *)urlRequest handler:(SeriouslyHandler)handl return self; } -- (void)start { - if (self.isCanceled || self.isFinished) return; - - if (![NSThread isMainThread]) { - [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO]; - return; - } - +- (void)main { + + if (self.isCanceled || self.isFinished) return; + KVO_SET(isExecuting, YES); - _connection = [[NSURLConnection alloc] initWithRequest:_urlRequest delegate:self]; + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + _connection = [[NSURLConnection alloc] initWithRequest:_urlRequest delegate:self]; [_connection start]; + + if (![NSThread isMainThread]) { + //keep the thread alive while connection is executing + //without this NSURLConnection delegate is not called + NSRunLoop *theRL = [NSRunLoop currentRunLoop]; + while ([self isExecuting] && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]){ + //nothing to do + } + } + + [pool drain]; + +} +- (void)start { + + [self main]; } - (void)cancel {