views:

191

answers:

1

I went to the iPhone Developer Tech Talk a few months ago and asked one of the gurus there about the lack of NSHost on the iPhone. Some code I was porting to the iPhone made significant use of NSHost throughout its networking code.

I was told that NSHost is on the iPhone, but its private. I was also told that NSHost is a synchronous API and that I shouldn't use it anyway. (If anyone could elaborate on why it shouldn't be used, as a bonus, that'd be great.)

I can see the caveats of using synchronous API's on the main thread in that they'll block until complete - and that's never a good thing with network code because there are so many factors that could cause the API to block the thread for a significant amount of time.

My solution was to write a wrapper around CFHost's asynchronous resolution functions.

The result works quite well, and I'm considering releasing it into the public domain.

But my question is this: Say my app only resolves a hostname once per run, during the connecting phase, and then cache's it for the rest of the session. During the time it is resolving, a modal screen is shown telling the user "Connecting" with a nice spinner.

Does it really matter whether or not the resolution is asynchronous?? The user has to wait to connect anyway, and the resolution is only done on the first connection. Subsequent connections use the cached result of the resolution.

When is it OK to be synchronous and when should things be asynchronous?

A: 

Your nice spinner won't spin, since the UI will get blocked as well during the synchronous call. Sure, you can make the call on a separate thread, but then you're doing essentially the same thing as the asynchronous call.

yakovlev
I'm not sure what it is exactly about UIActivityIndicator, but it seems to keep animating (spinning) even if the main thread is blocked, It's very weird. I can only imagine the UIActivityIndicatorClass does it's animation on another thread?? Strange.
Jasarien