iOS App Architecture with NSOperations iOS App Architecture with NSOperations objective-c objective-c

iOS App Architecture with NSOperations


yes, if it's being farmed out to service requests and you have many calls to make, then such a library is not (imo) overkill, and i have written something similar. this structure made it very easy for me to manage a complex system, with very complex and varied tasks.

the primary design difference i made was not to use NSNotification with a service manager. instead, i favored using protocols/types for callbacks (which the operation holds a reference to). NSNotification is quite heavy. in this case, the operation does not retain the listener(s)/notified objects, but the listeners retain the operation. if the relation is 1-1, then allow cancellation.

another major consideration is to define threading early on. allow clients to define which thread they want to receive their response on. the reason for this is that there is often a constraint or logical entry for the callback if the notified/listener must update the UI (e.g., you're using UIKit or AppKit). therefore, the creator can say to the operation 'you must inform me from the main thread', or 'i can handle the response from any thread'. this will reduce your controller/listener/observer code and chance for errors greatly.


For "sub operations": what about putting them on your queue, with the parent operation being a dependency (cf. -[ NSOperation addDependency: ]) of each of its child operations? The NSOperationQueue can sequence your whole pile of operations for you. I think this is simple and natural to work with.


You have just described a very similar architecture that I'm using in a few of my apps :)

I have my service manager layer returning a set of objects instantly and then returning an updated set after a while i.e.

NSArray *stuff = [serviceManager getFriendsForUser:@"Bob"];

and then, after the server has responded, an NSNotification is received that contains an updated list (of friends for Bob in this case).

Apart from this tiny change, your architecture is the same!

It is quite a lot of work to get it all set up but I think it's worth it in the long run as fixing bugs / extending the code is much easier.