TheDanman
TheDanman

Reputation: 98

Detecting when Asynchronous Connection has completed

I have a class with a method (getAndPlaySong) that gets an text file from a server, gets a specific string from the text, and uses the string (a URL) to get a music file from the server. The first server call to get the text file is quick so I implemented it synchronously. The second takes longer and I want to update the display while it is occurring.

I want the method to wait for the asynchronous request to finish before it proceeds. I have tried putting a BOOL in the connectionDidFinishLoading method:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %d bytes of data",[_receivedData length]);
    [AppDelegate playAudioWithData: _receivedData];
    _dataIsReceived = YES; //BOOL

    // release the connection, and the data object
    [connection release];
    [_receivedData release];
}

And then putting a while loop in the getAndPlaySong method:

[self startRequest: correctSongURL]; starts the request asynchronously

while (!_dataIsReceived) {
    //do stuff
}

The result is that the app hangs when it reaches the loop. The loop never terminates and I never get the "Succeeded..." output. I am a little confused and would be grateful for some input.

Upvotes: 0

Views: 1097

Answers (3)

Cliff Ribaudo
Cliff Ribaudo

Reputation: 9039

Your app is hanging on the Loop because it is infinite.

On iOS, NSURLConnectionDelegate is the way to get a call back when data is received on the connection and to be notified when it is done. And EVERYTHING should be done asynchronously and via the Delegate. That is THE way it is done on iOS.

The results as it streams in should be stored in an NSData object in the didRecieveData method.

Look at the docs on NSURLConnectionDelegate, but here is an outline example:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
     int statusCode = [(NSHTTPURLResponse *)response statusCode];    
     if(statusCode == 404) {
         // Post notification and let concerned parties know something went wrong.
         [self cleanupConnection];
     }
     else if( statusCode == 200) {
         // Success do something with the data.
         requestData = [[NSMutableData alloc] init];
     }
     else {
         [self cleanupConnection];
     }
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [requestData appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [self finishedLoadingAppData];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"Connection Attempt Failed with Error:%@", [error localizedDescription]);
}

Upvotes: 1

Scrungepipes
Scrungepipes

Reputation: 37581

I don't wish to sound rude, but what you have done is the worst possible thing. Of course the app hangs because it has an infinite loop. Even if the loop terminates eventually, while its in the loop your app is stuck - what if the user tries to do something with your app while its in the middle of the loop? You need to change your mindset and learn how to use event driven programming with a state machine. That's the paradigm of iOS, you have to knuckle down and do some reading and learning and change the way you think about and construct your programs.

After you make the request you do nothing, i.e. there is no code coming next in your code snippet. Instead your app sits there doing nothing (but still responsive) waiting for the connection to finish (while your app is doing nothing, the OS has created a thread on your behalf and the connection code is executing inside it, when its finished your connection code needs to inform the rest of your code it has finished, you could do this via a delegate for example, or via a notification).

And that is not the only thing it needs to sit and wait for - what if the user taps on the screen? What if the user exits your app? What if ... Your app needs to be able to responde to all possible events and respond quickly, the connection is just one more event it must listen for and respond to. It can't do that if its in a loop.

Never use loops anywhere like this.

"I want the method to wait for the asynchronous request to finish before it proceeds." No you don't - you can never do anything like this in an application with a GUI. The function must finish executing. You need to implement a state machine, every iOS is a state machine, the application delegate is the main thing driving the state machine, then you add more states and transitions between the state as more events get added to the program.

Look at the app delegate and its methods that tell your code the app entered the foreground, or the background, or there is a low memory situation etc. That's event driven programming, and you need to extend and build on that principle to take into consideration your connection events and any other events.

Sorry if I sound a bit harsh.

Upvotes: 2

Andy Obusek
Andy Obusek

Reputation: 12842

Two quick options come to mind: Create a method to call to handle the completed connection Use the NSNotification API to send a notification when the load is complete

Additionally, you can use the method for NSURLConnection didReceiveData to handle data coming incrementally from the server.

Upvotes: 0

Related Questions