Asynchronous networking library design



I’m facing a design decision while developing an iPhone app library for which I’d appreciate feedback for. Basically, I’m developing an asynchronous HTTP client library for a REST API.

My question is:
What’s the recommended way to return the parsed data from the library back to the calling code?

Synchronously would simply:

or for custom objects

I’m confused about how to do it cleanly asynchronously.

As usual, I googled around and ran across this unanswered question with the same exact question. The original poster found a solution but he/she is not sure whether is the recommended one.

Thanks for your help.



It’s deprecated now that the idiots in the Twitterverse pissed off Ben, but I use ASIHTTPREQUEST and it works great. It uses NSOperations to spawn async requests and returns back all you need.

I took it sone step further, I created a SyncServer class that I instantiate for each request. It sets up ASI, fires off the Async request, receives the data, parses it and then returns to the caller the fully populated Model Class from the JSON that was returned from the request. Same as sending stuff back to the server. I just fill the model, and call SyncServer to send the JSON back to the server using ASI. Same for retrieving images, jpegs, etc. I have a single call, like this in the method that wants an image.

[sometingFrame setImage:(UIImage *)[imageCache imageForKey:[entity serialSomethingImage] type:@“Something”]]; (Words changed for modesty)

[ul][li]imageCache comes mostly right from the BNR book that checks to see if I have that image named [entity serialSomethingImage] in the cache, if so it returns it.
[li]If not in the cache it then checks CoreData to see if I have a CDObject Blog image (transformable) for that image.
I keep images less than 150k in CoreData cause they are really small and easy to get to.
[li]If not in Coredata then it checks the file system to see if a file (larger than 150k) is on the iPhone/iPad.
[li]If not in the file system then it instantiates an instance of SyncServer and puts out an HTTP GET request to download the image Async from the server.
Once it get’s the image, it will either put it on the disk or in CoreData depending on it’s size, add it to the imageCache and then return the UIImage to the caller. [/li][/ul]

So from then on it’s in the cache which I release on ‘willEnterBackground’ or if I get a memory warning.


I also recommend checking out AFNetworking, as ASIHTTPRequest was discontinued by its main author. There may be an opensource community continuing work on it, but it’s waned recently.