We're using dataWithContentsOfURL because it is, uh, simple...
NSData *datRaw = [NSData dataWithContentsOfURL:ur];
Now, of course, that will hang the main UI thread.
So we put it on another thread. We do that exactly thus,
-(void)performSearch:(NSString *)stuff then:(void(^)(void))after
{
dispatch_queue_t otherThread =dispatch_queue_create(nil,0);
dispatch_queue_t mainThread =dispatch_get_main_queue();
dispatch_async(otherThread,
^{
self.resultsRA = [self ... calls dataWithContentsOfURL ...];
dispatch_async(mainThread, ^{ if (after) after(); });
});
}
(Incidentally, here's an excellent introduction to that if needed https://stackoverflow.com/a/7291056/294884).
Well now, Apple says you should not use dataWithContentsOfURL, they say you should instead just use NSSession. So, dataTaskWithURL:completionHandler:
My question, is there any difference at all between making our own thread (i.e. with dataWithContentsOfURL) versus using dataTask ?
Are we wrong to use dataWithContentsOfURL: on a thread, for some reason? I appreciate it is more convenient, etc. I mean is there are real difference, any dangers, etc.
One reason to prefer true async io over threaded synchronous io is that threads are not free memory-wise. It's not a huge deal in general but you can save a little memory in your app and (more importantly) a little wired memory in the OS's kernel by not keeping a thread sitting around doing nothing while it waits.