Re: How do I use NSStream with dispatch queues?
Re: How do I use NSStream with dispatch queues?
- Subject: Re: How do I use NSStream with dispatch queues?
- From: Raffi Khatchadourian <email@hidden>
- Date: Mon, 19 Mar 2012 19:10:31 -0700
Have you considered the -performSelectorOnMainThread method?
On Mar 19, 2012, at 11:59 AM, Jens Alfke wrote:
> I want to fix the threading of some increasingly-complex network code I’m writing[1]. Currently it all ends up running on the internal NSURLConnection thread (ick!) because it’s invoked by an NSURLProtocol instance. I want to move it all to a dispatch queue. But I’m finding out that it’s hard to wean existing code off of runloop-based asynchrony.
>
> My current sticking point is NSStream. Even though my code is all HTTP-based, I have to use NSStream to be able to stream large HTTP bodies (using the NSURLRequest.HTTPBodyStream property.) And unlike NSURLConnection, NSStream doesn’t have a way to hook its delegate up to a dispatch queue, only to a runloop.
>
> Here I get really worried. It seems as though I’d have to use some kludge like borrowing the main thread’s runloop to schedule my stream delegate on. But this means I’m not independent of the main thread anymore, so my network code could get stuck if the UI is busy (and conversely I might hurt UI responsiveness if my delegate takes too long.) It also sounds like this approach is subject to some weird intermittent threading glitches, as described in a blog post by Jamie Pinkham[2].
>
> But the alternative — not using dispatch queues — still seems really primitive. I’d have to spawn my own NSThread, start up a runloop on it, implement a custom thread-safe queue to hold request objects of some sort, and write some CFRunLoop glue to twiddle a runloop source so I can wake up the thread when a request is posted. I did it once before in 2005 and it was surprisingly hard to get right.
>
> It really seemed like GCD had fixed all of this, but now it looks to me like GCD support is still kind of half-baked, even several years after it showed up in 10.6. Or am I missing something?
>
> —Jens
>
> [1] https://github.com/couchbaselabs/TouchDB-iOS
> [2] http://jamiepinkham.com/post/4366476208/on-gcd-and-nsstreams
>
>
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Macnetworkprog mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden