Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discussion on 'didReceiveData' method for HTTP Connection

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?

like image 446
Chilly Zhong Avatar asked Sep 11 '26 08:09

Chilly Zhong


1 Answers

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.

like image 186
Peter Hosey Avatar answered Sep 14 '26 04:09

Peter Hosey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!