0

I have 2 separate NSURLConnection.

NSURLConnection * connection_users;
NSURLConnection * connection_cards;

Then i created the data with parameters, etc. and I finish with:

connection_users = [[NSURLConnection alloc] initWithRequest: url_request_users delegate: self startImmediately: YES];

In the delegate method:

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

i Checked if the connection is for the connection_users:

if (connection == connection_users) / / do something as an example:
NSDictionary * json_response = [NSJSONSerialization JSONObjectWithData: data options: kNilOptions error: & error];

Use the "data" that came from the method.

Before closing the "if" I create the next connection to "connection_cards", doing the same things

Out of "if" but within the same method I do another "if" to "connection_cards" and do the same thing with JSONObjectWithData.

Only the "data" that comes from the method is always of the first connection. What is happening differently? For the second connection was initiated then you should receive the "data" corresponding. Already canceled the first connection before starting the second to see if solved, but no.

How to obtain the "data" correct for second connection?

PS: if you need more codes, please let me know.

EDITED:

As Wain ask

url_request_users               = [[NSMutableURLRequest alloc] init];
NSMutableString *post_users     = [[NSMutableString alloc] init];
[post_users appendFormat:@"%@", [NSString stringWithFormat:@"email=%@&senha=%@",
                                             [[alert textFieldAtIndex:0] text],
                                             senha_md5]];

[url_request_users setURL:[NSURL URLWithString:WBS_USERS_RECOVER]];
[url_request_users addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[url_request_users setHTTPMethod:@"POST"];
[url_request_users setHTTPBody:[post_users dataUsingEncoding:NSUTF8StringEncoding]];

connection_users   = [[NSURLConnection alloc] initWithRequest:url_request_users delegate:self startImmediately:YES];
Daniel Arantes Loverde
  • 2,317
  • 2
  • 28
  • 41

1 Answers1

1

For n different connections you will need n different NSMutableData which contains result of related NSURLConnection. A basic example for your question;

NSMutableData *data_users;
NSMutableData *data_cards;

Than on your didRecieveData;

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if (connection == connection_users) {
        [data_users appendData:data];
    } else if ( connection == connection_cards) {
        [data_cards appendData:data];
    }  
}

This way you can keep track of your data's and connection's seperately. Remember to clear leftovers for your datas when your connection is over

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    if (connection == connection_users) {
        // use data from data_users
        NSDictionary * json_response = [NSJSONSerialization JSONObjectWithData:[data_users copy] options: kNilOptions error: & error];
        data_users = [[NSMutableData alloc] init]; // clear data users
    }
    // do the same for cards connection
}

Last thing to do is to allocate your data before you call this function;

url_request_users               = [[NSMutableURLRequest alloc] init];
NSMutableString *post_users     = [[NSMutableString alloc] init];
[post_users appendFormat:@"%@", [NSString stringWithFormat:@"email=%@&senha=%@",
                                             [[alert textFieldAtIndex:0] text],
                                             senha_md5]];

[url_request_users setURL:[NSURL URLWithString:WBS_USERS_RECOVER]];
[url_request_users addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[url_request_users setHTTPMethod:@"POST"];
[url_request_users setHTTPBody:[post_users dataUsingEncoding:NSUTF8StringEncoding]];

data_users = [[NSMutableData alloc] init]; // add this line in your code 
connection_users   = [[NSURLConnection alloc] initWithRequest:url_request_users delegate:self startImmediately:YES];
Bartu
  • 2,189
  • 2
  • 26
  • 50
  • let me know if you need further info – Bartu May 24 '13 at 22:48
  • Yes, i need. Where i do "JSONObjectWithData" if this receive only NSData not MUTABLEDATA ? – Daniel Arantes Loverde May 24 '13 at 23:09
  • JSONObjectWithData:[data_users copy]... copy method of an object will create a non-mutable copy of it. In reverse, you can get NSMutableData from NSData by calling [unmutableData mutableCopy] – Bartu May 24 '13 at 23:11
  • OK, but why using "didfinishloading" instead "didreceivedata" ? – Daniel Arantes Loverde May 24 '13 at 23:15
  • didRecieveData can be called multiple times per connection call. If you fetch a big data, you will see that didRecieveData gets called more than once (e.g. for your connection_cards). That's why you stack up your data on your didRecieveData until your connection is closed, and use/parse it on your connectionDidFinishLoading – Bartu May 24 '13 at 23:16
  • also I will suggest you to look at sendAsynchronousRequest:queue:completionHandler: at this link: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html. this can save you alot of code if you are going to create tens of connections. I used to use delegate approach and I am switching to async calls for each app I have. Less strugle, less code. But you need to read the whole documentation (and learn blocks) – Bartu May 24 '13 at 23:26
  • i left Async because it is only 2 data, receive 1 user, then, from the user, many cards. This must be NOT Async. But thanks! – Daniel Arantes Loverde May 24 '13 at 23:30
  • I edited it again, I put that comment for you to understand where you need to use data_users :) now I added that as well. Please don't copy/paste, try to understand what is going on and you will figure out the rest. – Bartu May 25 '13 at 00:09
  • Please, be nice. I do not copy and paste. Always try to understand the answers comparing my code and before closing the BUILD I read the documentation to see if the respondents are not trying to gain reputation. Your help was the explanation of "didRecieveData" who cleared more than documentation, so that's why you won the vote ;) – Daniel Arantes Loverde May 25 '13 at 02:07
  • @bartu use `resetBytesInRange` https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableData_Class/Reference/NSMutableData.html#//apple_ref/occ/instm/NSMutableData/resetBytesInRange: instead of allocing the NSMutableData from scratch (in order to clear it).. it's cleaner code – abbood May 25 '13 at 05:00