Question

In my app I need to download and post some data... First of all I need to download some data and then I need to do a post request. I Use async request to don't freeze the UI of my app... But when I call my method to post some data... I don't care about data returned from server. But the this method are called also when I do some post request.

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)aResponse
{
    NSLog(@"------------------------------- connectionDidReceiveResponse");
    expectedResponseLength = [NSNumber numberWithFloat:[aResponse expectedContentLength]];
    URLresponse = aResponse;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{   



    [self.responseData appendData:data];
}

How can I do my post request like this below without calling (automatic) this 2 methods (up) (used when I download info) and without freezing user GUI (I don't care data when I do post request but I need data in the 1st case)?

My post request is this:

- (void)postRequestWithURLState:(NSString *)url
{
    NSString *bodyRequest = nil;
    NSURL *requestURL = [NSURL URLWithString:url];
    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];

    //NSLog(@"-------------- bodyRequest: %@", bodyRequest);



    [theRequest setURL:requestURL];
    [theRequest setTimeoutInterval:2.0];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody:[bodyRequest dataUsingEncoding:NSASCIIStringEncoding]];
    [self.oauthAuthentication authorizeRequest:theRequest];
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:theRequest delegate:self];




    self.web = conn;




}
Was it helpful?

Solution

You can hold a reference to each of the different requests after performing them, then make some conditional code in the delegate methods that does something different for the two.

That's a rudimentary solution and feels like treating symptoms to me. Maybe you should refactor your approach and create controllers for each of the two operations and perform all of network communication there (it seems like you're doing it all in a view controller now) rather than where you're doing it now.

OTHER TIPS

I was looking around the internet for a solution, I eventually just created my own subclass of NSURLConnection and assigned a tag variable to distinguish. Check out this blog post for more information

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top