0
votes

I have a JSON request that gets sent to a server based on the response of another JSON request, I used this method to check what NSURL connection is being used to handle the response:

    if(connection == connection1) {
    // Connection 1
    } else if(connection == connection2) {
    // Connection 2
    }

It works for the first connection, which is sent out like this:

    NSString *jsonStr = [NSString stringWithFormat:@"https://api.flightstats.com/flex/flightstatus/rest/v2/json/flight/status/%@/%@/dep/%@?appId=%@&appKey=%@&utc=false",airline.text,flightnumber.text,datestring,flightAPIIDstring,flightAPIkeystring];
    NSURL *jsonurl = [NSURL URLWithString:jsonStr];
    flightTrackJSONconnectionrequest = [NSURLRequest requestWithURL:jsonurl];
    flighttrackJSONconnection =[[NSURLConnection alloc]
                                initWithRequest:flightTrackJSONconnectionrequest delegate:self];
    if(flighttrackJSONconnection){
    jsonresponse = [[NSMutableData alloc] init];
    } else {
    }

However, on the connectionDidFinishLoading for my first connection (flighttrackJSONconnection), when I use this code, the request for the 2nd connection is not sent out at all:

    NSString *departjsonStr = [NSString stringWithFormat:@"https://api.flightstats.com/flex/airports/rest/v1/json/iata/%@?appId=%@&appKey=%@",DepartACode,flightAPIIDstring,flightAPIkeystring];
    NSURL *departjsonurl = [NSURL URLWithString:departjsonStr];
    departairportcheckJSONconnection = [NSURLRequest requestWithURL:departjsonurl];
    departairportcheckJSONconnection =[[NSURLConnection alloc]
                                           initWithRequest:departairportcheckJSONconnectionrequest delegate:self];
    if(departairportcheckJSONconnection){
    departairportlookup = [[NSMutableData alloc] init];
    } else {
    }

I added NSLog checks to the connection didFailWithError, connection didReceiveResponse and connectionDidReciveData all to find that the only NSURL request going out was my flighttrackJSONconnection request...

So I'm wondering, did I miss something in my code, or is it simply not possible to send another NSURLRequest when it calls connectionDidFinishLoading?

Sorry if I seem like a noob, I'm just confused a bit...

Also, all the JSON parsing code (at least for the flighttrackJSONresponse connection) and such works fine on connectionDidFinishLoading, so its not like I made a mistake declaring anything & if it makes any difference, I'm trying to send 2 requests on the connectionDidFinishLoading, each of them with their own NSURLConnection and NSURLRequest names declared on the .h, I have the same issue with both.

Thanks for any tips or ideas guys..

1
Do you send the second connection based in a parameter of the fisrt json response? - CainaSouza
Yeah I did, is that what might be the problem? - Jon Sullivan
I posted an answer (as a suggestion). Check it out! - CainaSouza
Are you sure you are not calling [flighttrackJSONconnection start] method and still the request goes? - Shashank
Yeah, the only thing that doesn't work is the actual request, I didn't show the code, but before the request is sent it pulls an API key from a server, I tested it and the API key gets pulled fine, the only issue is it doesn't want to send the request. - Jon Sullivan

1 Answers

1
votes

I would recommend using the method sendAsynchronousConnection, like this code:

NSURL *url = [NSURL URLWithString:urlString];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{

    // Treat your response here
}];

Inside the block you treat your response. Parse your JSON, check what need to be done and call this same method inside it! Using this, you don't need to implement delegate methods.