37

I've got the problem when I tried to do asynchronous requests to server from background thread. I've never got results of those requests. Simple example which shows the problem:

@protocol AsyncImgRequestDelegate
-(void) imageDownloadDidFinish:(UIImage*) img;
@end


@interface AsyncImgRequest : NSObject
{
 NSMutableData* receivedData;
 id<AsyncImgRequestDelegate> delegate;
}

@property (nonatomic,retain) id<AsyncImgRequestDelegate> delegate;

-(void) downloadImage:(NSString*) url ;

@end



@implementation AsyncImgRequest
-(void) downloadImage:(NSString*) url 
{  
 NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:url]
             cachePolicy:NSURLRequestUseProtocolCachePolicy
            timeoutInterval:20.0];
 NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
 if (theConnection) {
  receivedData=[[NSMutableData data] retain];
 } else {
 }  

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  [delegate imageDownloadDidFinish:[UIImage imageWithData:receivedData]];
  [connection release];
  [receivedData release];
}
@end

Then I call this from main thread

asyncImgRequest = [[AsyncImgRequest alloc] init];
asyncImgRequest.delegate = self; 
[self performSelectorInBackground:@selector(downloadImage) withObject:nil];

method downloadImage is listed below:

-(void) downloadImage
{
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 [asyncImgRequest downloadImage:@"http://photography.nationalgeographic.com/staticfiles/NGS/Shared/StaticFiles/Photography/Images/POD/l/leopard-namibia-sw.jpg"];
 [pool release];
}

The problem is that method imageDownloadDidFinish is never called. Moreover none of methods

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response

are called. However if I replace

 [self performSelectorInBackground:@selector(downloadImage) withObject:nil]; 

by

 [self performSelector:@selector(downloadImage) withObject:nil]; 

everything is working correct. I assume that the background thread dies before async request is finished it job and this causes the problem but I'm not sure. Am I right with this assumptions? Is there any way to avoid this problem?

I know I can use sync request to avoid this problem but it's just simple example, real situation is more complex.

Thanks in advance.

Micah Hainline
  • 14,367
  • 9
  • 52
  • 85
Dmytro
  • 2,522
  • 5
  • 27
  • 36

3 Answers3

59

Yes, the thread is exiting. You can see this by adding:

-(void)threadDone:(NSNotification*)arg
{
    NSLog(@"Thread exiting");
}

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(threadDone:)
                                             name:NSThreadWillExitNotification
                                           object:nil];

You can keep the thread from exiting with:

-(void) downloadImage
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [self downloadImage:urlString];

    CFRunLoopRun(); // Avoid thread exiting
    [pool release];
}

However, this means the thread will never exit. So you need to stop it when you're done.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    CFRunLoopStop(CFRunLoopGetCurrent());
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    CFRunLoopStop(CFRunLoopGetCurrent());
}

Learn more about Run Loops in the Threading Guide and RunLoop Reference.

nall
  • 15,899
  • 4
  • 61
  • 65
  • After researching for many hours (8+) on how to perform an asynchronous NSURLConnection within an NSInvocationOperation, none of the solutions were as elegant as CFRunLoopRun() and CFRunLoopStop(). This is MUCH better than messing around with "isExecuting" instance variables using the KVO protocol. One thing to note is that I used @selector(performSelectorOnMainThread:withObject:waitUntilDone:modes:) passing [NSArray arrayWithObject:NSDefaultRunLoopMode] as the modes. Main thread to satisfy UIKit requirements, NSDefaultRunLoopMode to keep the UI interaction smooth. Thanks a lot! – James Wald Dec 19 '09 at 10:40
  • 6
    For projects compiled with ARC, `NSAutoReleasePool` is not available, so the code would look like `@autoreleasepool { [self downloadImage:urlString]; CFRunLoopRun(); }` – alokoko Mar 06 '12 at 21:12
  • Can we stop this background thread from main thread if I want to stop downloading? – Khushbu Shah Apr 24 '13 at 09:03
  • As per the Apple Threading Programming Guide, "if you use a thread to perform some long-running and predetermined task, you can probably avoid starting the run loop." – Harshal Chaudhari Jun 08 '16 at 14:38
1

You can start the connection on a background thread but you have to ensure the delegate methods are called on main thread. This cannot be done with

[[NSURLConnection alloc] initWithRequest:urlRequest 
                                delegate:self];

since it starts immediately.

Do this to configure the delegate queue and it works even on secondary threads:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:urlRequest 
                                                              delegate:self 
                                                      startImmediately:NO];
[connection setDelegateQueue:[NSOperationQueue mainQueue]];
[connection start];
MacMark
  • 6,239
  • 2
  • 36
  • 41
0

NSURLRequests are completely asynchronous anyway. If you need to make an NSURLRequest from a thread other than the main thread, I think the best way to do this is just make the NSURLRequest from the main thread.

// Code running on _not the main thread_:
[self performSelectorOnMainThread:@selector( SomeSelectorThatMakesNSURLRequest ) 
      withObject:nil
      waitUntilDone:FALSE] ; // DON'T block this thread until the selector completes.

All this does is shoot off the HTTP request from the main thread (so that it actually works and doesn't mysteriously disappear). The HTTP response will come back into the callbacks as usual.

If you want to do this with GCD, you can just go

// From NOT the main thread:
dispatch_async( dispatch_get_main_queue(), ^{ //
  // Perform your HTTP request (this runs on the main thread)
} ) ;

The MAIN_QUEUE runs on the main thread.

So the first line of my HTTP get function looks like:

void Server::get( string queryString, function<void (char*resp, int len) > onSuccess, 
                  function<void (char*resp, int len) > onFail )
{
    if( ![NSThread isMainThread] )
    {
        warning( "You are issuing an HTTP request on NOT the main thread. "
                 "This is a problem because if your thread exits too early, "
                 "I will be terminated and my delegates won't run" ) ;

        // From NOT the main thread:
        dispatch_async( dispatch_get_main_queue(), ^{
          // Perform your HTTP request (this runs on the main thread)
          get( queryString, onSuccess, onFail ) ; // re-issue the same HTTP request, 
          // but on the main thread.
        } ) ;

        return ;
    }
    // proceed with HTTP request normally
}
bobobobo
  • 64,917
  • 62
  • 258
  • 363