A good way to do this would be to use blocks. Your doLookup:
method can accept a block object, and you can call it when the connection ends.
Since you probably want to be able to execute multiple requests with different completion blocks, you need to associate the passed in block with the corresponding NSURLConnection.
To do this, you can either use the NSURLConnection
subclass with the completionBlock
property, or use the objective-C related objects (using the objc_setAssociatedObject
and objc_getAssociatedObject
) to bind the block to the connection object.
When everything is ready in the connectionDidFinishLoading:
method, and you have prepared the final response object, you grab the block from the NSURLConnection
object and call it, passing it the final data.
So, you end up wanting your client code to look like this:
[feeder doLookup:@"Something" completionBlock:(FetchedData *data, NSError *error) { if (error) {
I hope this was detailed enough for you.
source share