My runloop-based async code breaks with GCD
My runloop-based async code breaks with GCD
- Subject: My runloop-based async code breaks with GCD
- From: Jens Alfke <email@hidden>
- Date: Fri, 17 Feb 2012 16:22:53 -0800
I’m trying to take some ordinary single-threaded Cocoa code, and make it possible to invoke it from multiple threads. At this point I’m not modifying this code to add thread-safety to it; instead I just want to allow asynchronous calls into it, which will always run on a single thread at a time. I’m trying to do this by wrapping the calls in blocks that will execute serially.
The trouble I’m having is that neither GCD dispatch_queues nor NSOperationQueues seem to have runloops, so any code that explicitly or implicitly uses runloops for scheduling breaks when it’s invoked from a block running on a dispatch or operation queue. This is breaking that code that I wanted to avoid altering, and in weird ways (it doesn’t crash, it just ‘forgets’ to complete async operations.)
The specific problems I’m hitting so far are:
• NSURLConnection doesn’t ever call its delegate methods, unless you call its -setDelegateQueue: method. (Which requires an NSOperationQueue, so it only works if you’re using those, not if you use dispatch queues directly.)
• -[NSObject performSelector:withObject:afterDelay:] doesn’t work. No exceptions, but it never performs the selector. I haven’t figured out a workaround for this yet.
What concerns me the most is that it seems like there are now at least three different incompatible operating environments for working with asynchronous operations: a normal thread with a runloop, an NSOperation, and a block invoked directly from a dispatch queue. If you want to schedule a call after a delay, or a delegate callback that should be invoked later, the code doing so has to figure out somehow what mode it’s in and make different API calls accordingly. That’s going to complicate code that wants to be reusable, and it means that code I’ve written over the past decade in the normal runloop environment is going to break in subtle and hard-to-debug ways with GCD unless I fix it to handle all of these situations.
…or am I just missing something obvious that makes this all work?
—Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden