Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLConnection sendSynchronousRequest/sendAsynchronousRequest fails after app killed by iOS

All the server connections in my app goes the same way:

  • dispatch_async to a 2nd thread.
  • sendSynchronousRequest
  • fail/success blocks on main thread.

  • The following is not the full code, just the concept:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *url = [NSURL URLWithString:@"someURL"];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        NSURLResponse *response = nil;
        NSError *error;
        NSData *data = [NSURLConnection sendSynchronousRequest:request
                                                 returningResponse:&response
                                                             error:&error];
       if (error)
       {
          dispatch_async(dispatch_get_main_queue(), ^{ _failureBlock(error); });
       }
       else
       {
          dispatch_async(dispatch_get_main_queue(), ^{ _successBlock(data); });
       });
    

Well the app works great! I can use it for thousands of server connections, and it always works when I run a fresh load of the app. The issue starts when the following occurs:

  1. Send the app to BG.
  2. Wait some time...
  3. The app get killed by the iOS.
  4. Open again, the iOS loads the whole app from scratch. (see splash screen followed by the first screen, not the one I left to BG..)

In that scenario I get NSURLConnectionErrors!! All kind of them! Code 1003, Code 1009, Code 9... Just name it! I get errors for all server connections that starts right when the app loads. Any other connections after that works fine! including the refresh of the ones that got failed!

It almost feel like i'm trying to reach my server too quick or too early, BUT I do check and pass a reachabillity test before intiating a connection.

Could really use some help here. Thanks in advance.

UPDATE: As suggested below, tried to use sendAsynchronousRequest - gave me the exact same behaviour. (err: 1001 this time).

like image 690
Mute Dan Avatar asked Dec 24 '12 00:12

Mute Dan


2 Answers

OK got it! The appDelegate built the tabBar wich loaded all viewControllers wich sent some NSURLConnections... So for the iOS thought it's too long to wait for the responses and failed them all! (No idea way the first launch is ok and only after the app killed its not)

Anyway, I changed all server loads to perform with selector with 0.1 delay. That way the appDelegate could finish running and all is GOOD! :)

like image 151
Mute Dan Avatar answered Nov 17 '22 10:11

Mute Dan


You should look at using this method and send the data Asynchronously

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)

the NSOpertationQueue mainQueue uses the mainthread however allowing you to update UI also on the main thread i.e an activityindicator for example..

like image 29
Alex McPherson Avatar answered Nov 17 '22 10:11

Alex McPherson