-1

I'm using NSURLSession to post a request. Although I'm setting queue to main queue still it takes too much time to respond

 NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:[NSOperationQueue mainQueue]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:LOGIN_SERVICE]];

[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];

NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];

[dataTask resume];

There is a similar question but it has only one answer which is similar to what I'm doing. Anything that I'm missing ?

Community
  • 1
  • 1
Misha
  • 685
  • 8
  • 20

2 Answers2

1

As you update question with code required, I will say nothing apparently here which make your request slow, few things as this is Network request, it will depend on different things like network speed, server response time. It would be worth while testing on different networks devices.

Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
0

You can use AFNetworking. If your response like this

{
  "my_response": {"name": "XXX","area": "XXX","num": "XXX"
  },
  "other_response": {"message": "Hello","status": "success","flag_value": "1"
  }
}

Stpe 1 :- And set time interval also.

- (void)yourMethod{
    NSString *urlString = [NSString stringWithFormat:@"%@", your_service_url];
    NSURL *url = [NSURL URLWithString:urlString];

   AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
   [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

   NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:

                           your_parameters_list,
                            nil];
        NSMutableURLRequest *jsonRequest = [httpClient requestWithMethod:@"POST"
                                                                path:urlString
                                                          parameters:params];

    [jsonRequest setTimeoutInterval:120];

    AFJSONRequestOperation *operation =
    [AFJSONRequestOperation JSONRequestOperationWithRequest:jsonRequest success: ^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        NSLog(@" Success %@", JSON);

        NSDictionary *jsonDictionary1 = [JSON valueForKey:@"my_response"];
        NSDictionary *jsonDictionary2 = [JSON valueForKey:@"other_response"];

                NSString* name = [jsonDictionary1 valueForKey:@“name”];
                NSString* area = [jsonDictionary1 valueForKey:@"name"];
                NSString* num =  [jsonDictionary1 valueForKey:@"num"];




    } failure: ^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Fail %@", [error userInfo]);

        NSLog(@“Error %@", [error localizedRecoverySuggestion]);


    }];

    [operation start];
}
Waruna
  • 162
  • 8