views:

305

answers:

2

Hi,

I am working on an application where the user at some point must wait for a proper response from a webservice. This might take some time, because it requires a manual confirmation from the webservice. Because of this, the timeoutInterval on the request is set very high to prevent it from terminating too early.

The problem comes when starting the application immediately after the program has returned to the home screen. The application will not start (black screen), and I think it is because the request hasn't been released and is still waiting for a response (I might be wrong though).

I tried the applicationWillTerminate method, but it isn't called when pressing the home button. Again, this might be because the application is still waiting for a response, but a better explanation would be greatly appreciated :)

Also, does someone have any idea on what to do?

code:

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:urlAdress]
                                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                      timeoutInterval:10000];

    NSHTTPURLResponse* response = nil;  
    NSError* error = nil; 

    NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest   
                                                             returningResponse:&response   
                                                                         error:&error]; 
+2  A: 

Making a synchronous request on the main thread will block the entire run loop until you receive a response or the request times out. Nothing will get called during this time, including applicationWillTerminate. It is a very bad idea to do this. You should either detach a separate thread for the request or create an asynchronous NSURLConnection.

That being said, the OS will free all memory anyway when it terminates your app so there is little chance that anything from the last launch will be "left over" upon relaunch. Unless your server is blocked because it lost its client when your app terminated. In which case you have to write your server in a way that can handle such a case because it can happen anytime.

Ole Begemann
You were right, it was a bad thing to do it like that :P I had to rewrite my code on the server.. thanks for your response :)
Madoc
A: 

If the applicationWillTerminate delegate method is not called,

Then I guess the following delegate method would be getting called.

- (void)applicationWillResignActive:(UIApplication *)application
{
  NSLog(@"Application Did Resign Active");
}
Biranchi