0

I am making multiple calls to server, I am using for loop for it

for(int i = 0; i <count; i++)
{

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:finalJson options:NSJSONWritingPrettyPrinted error:&error];
            if (!jsonData) {
                NSLog(@"Error creating JSON object: %@", [error localizedDescription]);
            }

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"httpLink"]];


            [request setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
            [request setValue:APIKEY forHTTPHeaderField:@"X_API_KEY"];

            [request setHTTPMethod:@"POST"];
            [request setHTTPBody:jsonData];

            m_dataPush = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

            [m_dataPush start];

}

For example suppose the count is 3, then the "m_dataPush" is called thrice

So now I have the delegate methods to handle the response.

But in my "connectiondidfinishLoading" function is called only once, and I am only getting one response.But in fact I should have got three responses.

Here are my delegate functions

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{

    if(connection == m_dataPush || connection == m_dataPull)
    {
        NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
        m_responseCode = [httpResponse statusCode];//Get status code for response

        m_ResponseData = [[NSMutableData alloc] init];
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    // Append the new data to the instance variable you declared


    if(connection == m_dataPush || connection == m_dataPull)
    {
        [m_ResponseData appendData:data];
    }
}


- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    // The request is complete and data has been received
    // You can parse the stuff in your instance variable now


    if(connection == m_dataPush || connection == m_dataPull )
    {
        NSDictionary *response = [NSJSONSerialization JSONObjectWithData: m_ResponseData options: 0 error: &e];  //I am using sbjson to parse

    }   
}

So please help me out, in how to manage this

Regards

Ranjit
  • 4,576
  • 11
  • 62
  • 121
  • You can add all your requset in an array then create a method that will fire them one by one. – Himanshu Sep 04 '14 at 11:44
  • Hey Hi @Himanshu, what is wrong with my approach – Ranjit Sep 04 '14 at 11:49
  • I usually work with nsurlsession and in that this approch will overwriting older request. I think its same issue here. – Himanshu Sep 04 '14 at 11:53
  • @Himanshu, ok, then how to handle responses received from the requests, above you can see my code – Ranjit Sep 04 '14 at 12:10
  • @Himanshu, your comment is not visible – Ranjit Sep 04 '14 at 12:38
  • Ok, You can do one thing create two methods. Lets say first is GetRequest And second is FireRequest. GetRequset will take an APi and an identifier(simple int) and store them as dictionary in array. And you will call only GetRequset method. And You have to take BOOL as isAPiRunning default is NO. Now you will Call GetRequset and it will save data in array at object at index 0 (always). And it will call FireRequet if isApiRunning is NO. Now your Arrayrequest is ready. – Himanshu Sep 04 '14 at 12:41
  • You will call FireRequest in all Delegates of NSURLConnection. And these respone will be return to you by custom Delegate with identifire. FireRequset will be called for array objectatindex 0. (always) BOOL will be true in FireRequest and will be false when any of delegate is called – Himanshu Sep 04 '14 at 12:42

0 Answers0