NSURLConnection 5 seconds delay

2019-09-04 03:09发布

问题:

i have the following code to make requests to my api:

-(void)makeApiCall:(NSString *)function params:(NSDictionary *)params notificationName:(NSString *)notificationName
{    
NSURL *url = [NSURL URLWithString:kBaseUrl];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
httpClient.parameterEncoding = AFFormURLParameterEncoding;

NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:[NSString stringWithFormat:@"%@/%@",kApiBaseUrl,function] parameters:params];


NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSLog(@"complete");
//..My code        
}];    
}

My problem is that, after the NSLog(@"complete"), the app is frozen for 5 seconds.. How can i fix that?

Thanks.

回答1:

The queue parameter of sendAsynchronousRequest is the queue on which the completion handler block is dispatched. If you do any UI updates in the completion handler then you probably should call the method with

queue:[NSOperationQueue mainQueue]