Vishaka Watal
Vishaka Watal

Reputation: 47

App Freezes when Network connection is slow,Because a POST API Call happens on APP launch. Below is my Connection Manager code,Objective-C, iOS

//Below is the code i have used to perform simple httpPOST. But app hangs on App launch on splash screen and crashes . i am doing an API Call on applaunch in Appdelegate

- (NSDictionary *)postUserRegWithUrl:(NSString *)urlString andParams:(NSString *)paramString{
NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                                                 NULL,
                                                                                                 (CFStringRef)paramString,
                                                                                                 NULL,
                                                                                                 (CFStringRef)@"+",
                                                                                                 kCFStringEncodingUTF8 ));
NSDictionary *responseDictionary;
NSError *err;

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kBaseProdURL,urlString]]];

NSData *data = [encodedString dataUsingEncoding:NSUTF8StringEncoding];
[request setTimeoutInterval:60.0];
 [request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
NSLog(@"the data Details is =%@", request);    
NSURLResponse *response;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];

NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

NSLog(@"got response==%@", resSrt);

if(resSrt.length)
{

    responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&err];

    NSLog(@"Response dictionary formed is =%@", responseDictionary);
} else {

    NSLog(@"failed to connect");

        [self showAlertViewTitle:@"Please try later" withMessage:@"Something went wrong"];

}
return responseDictionary;

}

Upvotes: 0

Views: 174

Answers (2)

Subramanian P
Subramanian P

Reputation: 4375

First set the timeoutInterval for your request. if your request takes more time then you have to stop the api call and inform the user with proper error message.

For example:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1200.0];

Don't use Synchronised Request. It will block your main thread.

If your network is slow or server is not responding then your app will take more time to load. Which is not good for the user experience.

Remember, your app’s load time is your first chance to impress your users.

Use Asynchronised Request of the NSURLConnection. Handle the response in the api completion block.

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSString *resSrt = [[NSString alloc]initWithData:data encoding:NSASCIIStringEncoding];

        NSLog(@"got response==%@", resSrt);

        if(resSrt.length)
        {
            responseDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&err];

            NSLog(@"Response dictionary formed is =%@", responseDictionary);
        } else {

            NSLog(@"failed to connect");
        }
    }];

Change queue:[NSOperationQueue mainQueue] parameter based on your need.

queue -> An NSOperationQueue upon which the handler block will be dispatched.

Upvotes: 2

mag_zbc
mag_zbc

Reputation: 6982

You shouldn't execute your network calls synchronously, especially on main thread. Either use sendAsynchronousRequest or just use any good networking library, like AFNetworking, which do this out of the box.

Upvotes: 3

Related Questions