Asynchronous iOS Network Library Design

I came across a design decision when developing an application library for the iPhone, for which I will be grateful for the feedback. Basically, I am developing an asynchronous HTTP client library for the REST API.

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

It will be simple synchronously: Users NSArray * = [restLibrary getAllUsers] where getAllUsers is declared as: - (NSArray *) getAllUsers;

or for custom objects User * tom = [restLibrary getUser: @ "tom"] if getUser: is declared as: - (User *) getUser: (NSString *) username;

I am confused about how to do this purely asynchronously.

I met this unanswered question with the exact same question. The original poster found a solution, but he / she is not sure if this is recommended.

Thank you for your help.

Jorge

+3
source share
2 answers

When using asynchronous calls, you are waiting for a callback. This basically means that you ask the class to start doing something, and when it does, it should execute the method of your choice.

A good example of this is Apple's own NSUrlConnection class. You provide this class to the delegate. This is basically an object that the NSUrlConnection class will execute methods as needed.

So,

NSURLRequest *  req = [NSURLRequest requestWithURL: self.url];
self.downloadConnection = [NSURLConnection connectionWithRequest: req
                                                        delegate: self];
[self.downloadConnection start];

NSUrlConnection self.url .

, , , ,

- (void) connectionDidFinishLoading: (NSURLConnection *) connection
{

}

. @protocol , . ,

- (void)userFetched:(User *)user

restLibrary ,

[restLibrary getUser:@"tom" delegate:self]

userFetched.

getUser restLibrary ( , NSUrlConnection User), userFetched .

..

?

+9
+2

All Articles