Does NSURLSession for HTTP data task (NSURLSessionDataTask) runs in background thread or we will have to provide the queue? Does NSURLSession for HTTP data task (NSURLSessionDataTask) runs in background thread or we will have to provide the queue? multithreading multithreading

Does NSURLSession for HTTP data task (NSURLSessionDataTask) runs in background thread or we will have to provide the queue?


No, you don't need to use GCD to dispatch this to background queue. In fact, because the completion block runs on background thread, the exact opposite is true, that if you need anything in that block to run on the main queue (e.g., synchronized updates to model objects, UI updates, etc.), you have to manually dispatch that to the main queue yourself. For example, let's imagine that you were going to retrieve a list of results and update the UI to reflect this, you might see something like:

- (void)viewDidLoad {    [super viewDidLoad];    NSURLSession *session = [NSURLSession sharedSession];    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString:@"https://itunes.apple.com/search?term=apple&media=software"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {        // this runs on background thread        NSError *error;        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];        // detect and handle errors here        // otherwise proceed with updating model and UI        dispatch_async(dispatch_get_main_queue(), ^{            self.searchResults = json[@"results"];    // update model objects on main thread            [self.tableView reloadData];              // also update UI on main thread        });        NSLog(@"%@", json);    }];    [dataTask resume];}