Send sendSynchronousRequest using NSURLConnection
When we create an NSURLConnection and send sendAsynchronousRequest and application get stuck or hang. So its a synchronous method – i.e. it runs on the Main Thread. Therefore, your application will hang – and it’ll wait till the data is downloaded.To prevent it: use the sendAsynchronousRequest method. It’ll run this process on a background thread and won’t disrupt your application or simply – won’t make it hang. how to use : –
1 2 3 4 5 6 7 8 |
NSURL *url = [NSURL URLWithString:@"YOUR_URL_HERE"]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { //the NSData in the completion handler is where your data is downloaded. }]; |