I've been trying to move away from targeting specific threads and thinking more about queues like suggested by best practices and guidelines in the iOS arena.
WHAT I USED TO DO: I used to be able to create a new thread, then run a runloop on it. Every time I wanted a task to be run on that thread I would call performselector:onThread:. That gave me a way to target a specific thread and more importantly to know that the work was going to to be associated with the NSRunLoop I started.
WHAT I'VE SEEN PEOPLE DO NOW WITH QUEUES AND NSRUNLOOPs: I saw this post on stack overflow: iOS, NSURLConnection: Delegate Callbacks on Different Thread?
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSRunLoop *loop = [NSRunLoop currentRunLoop];
[connection scheduleInRunLoop:loop forMode:NSRunLoopCommonModes];
[loop run]; // make sure that you have a running run-loop.
});
I don't see how this could address what I used to do with threads. Why? This is what I'm thinking and I would like to know if what I'm thinking is right or to see if I'm missing some major conceptual point that makes me think that this does not allow me to do with queues what I used to do with threads.
In my understanding GCD manages threads for the user. So, by calling:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),.... I am not actually able to know what thread is actually associated with the NSRUNLOOP that is started in the block here above. So, if I am thinking this right, GCD picks a thread when I go on a queue, so then how can I leverage the same runloop I started the last time I was put on the global queue by GCD?
I used to have a static variable for a specific thread, who then had an entry point to the NSRUNLOOP. Now, that I only want to deal with queues, how could I say "go on thread A, do a bunch of work" knowing that that thread a specific runloop I started?
So, I am assuming that the approach of doing:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSRunLoop *loop = [NSRunLoop currentRunLoop];
[connection scheduleInRunLoop:loop forMode:NSRunLoopCommonModes];
[loop run]; // make sure that you have a running run-loop.
});
would create a new runloop every time I do that. Instead, I want to be able to have only one runloop I do work on.
Can anybody tell me how to get work done on a specific runloop (other than the one used by the main thread) using queues instead of NSThreads, and clarify this whole discussion I made up here?
Thank you