I am using NSURLSession
to get some information from my server.
After i get the data, i show it in a UIAlertView
, but it takes too long to show. I print the data with the NSLog
and it prints it almost instantly... So what is happening? Does the method dataTaskWithRequest
is not asynchronous? Why it takes so much time to pop up the alert view?
NSURLSession *session;
session = [NSURLSession sharedSession];
NSURL * url = [NSURL URLWithString:[DRESSABLE_IP stringByAppendingString:@"index.php"]];
NSMutableURLRequest * urlRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:CONNECTION_CACHEPOLICY timeoutInterval:CONNECTION_TIMEOUT];
[urlRequest setHTTPMethod:@"POST"];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *total = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
[[[UIAlertView alloc] initWithTitle:@"Title" message:total delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
}];
All UI activity must be done in the main queue.
NSURLSession *session;
session = [NSURLSession sharedSession];
NSURL * url = [NSURL URLWithString:[DRESSABLE_IP stringByAppendingString:@"index.php"]];
NSMutableURLRequest * urlRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:CONNECTION_CACHEPOLICY timeoutInterval:CONNECTION_TIMEOUT];
[urlRequest setHTTPMethod:@"POST"];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *total = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(),^{
[[[UIAlertView alloc] initWithTitle:@"Title" message:total delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
});
}];