views:

1206

answers:

4

I created an indenpendent class for HTTP connection. All the connection works fine. The problem is that I find method 'didReceiveData' will be called AFTER the method who call the connection. (method 'didReceiveData' will be called after IBAction 'accept')


- (IBAction)accept:(id)sender {
    [self connect:url];
    //labelStr = ReturnStr; Cannot be written here. 
}

-(void)connect:(NSString *)strURL
{
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:strURL]
                cachePolicy:NSURLRequestUseProtocolCachePolicy
               timeoutInterval:60.0];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) 
    {
     // receivedData is declared as a method instance elsewhere
     receivedData = [[NSMutableData data] retain];
    } 
    else 
    { 
     // inform the user that the download could not be made
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    // append the new data to the receivedData
    [receivedData appendData:data];
    ReturnStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

This will cause a problem that if I want to change the text of a label to the received string, the code cannot be written in IBAction 'accept' but have to be written in method 'didReceiveData' like this:


    MainViewController *mainView = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
    AMEAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
    [delegate.navController pushViewController:mainView animated:YES];
    mainView.labelStr.text = ReturnStr;

A further problem is that the data on MainView will be overwritten if I initialize MainView in 'didReceiveData'. Is it possible for me to change the text of labelStr without initialize MainView?

+2  A: 

The problem is that I find method 'didReceiveData' will be called AFTER the method who call the connection. (method 'didReceiveData' will be called after IBAction 'accept')

You're expecting the connection to send you connection:didReceiveData: before you create and connect it?

This will cause a problem that if I want to change the text of a label to the received string, the code cannot be written in IBAction 'accept' but have to be written in method 'didReceiveData' …

Sounds about right. You can't work with something you've received until you've received it.

A further problem is that the data on MainView will be overwritten if I initialize MainView in 'didReceiveData'. Is it possible for me to change the text of labelStr without initialize MainView?

Creating the main view controller and app delegate in your connection:didReceiveData: method seems really late to do that. Do those things earlier, then have connection:didReceiveData: do nothing but set labelStr.text.

BTW, the implementation of connection:didReceiveData: that you show leaks ReturnStr. Remember to release or autorelease what you have allocked.

Peter Hosey
A: 

NSURLConnection and other similar classes are designed to be used asynchronously.

initWithRequest:delegate: returns immediately, and you aren't annoyed with the connection stuff till it sends the delegate methods to its delegate.

naoki
A: 

If you want your app to wait until the data comes in, use NSURLConnection's sendSynchronousRequest:returningResponse:error: method. Note, however, that the rest of your app will be frozen while this method is being run, and of course the method could take a while if the user has a crappy connection.

Brent Royal-Gordon
How to shut down the connection when loading finishes? Are there anything like didFinishLoading method.
iPhoney
With sendSynchronousRequest:returningResponse:error:, this is all managed by that method. By the time the method returns, the connection has been opened, the request sent, the response received, and the connection closed again. All that might take a few seconds, though.
Brent Royal-Gordon
A: 

Using NSMutableData instead of NSData.

Hi new-soul, the receivedData is already declared as NSMutableData.
iPhoney