iPhone - Architecture for viewController and network requests iPhone - Architecture for viewController and network requests objective-c objective-c

iPhone - Architecture for viewController and network requests


Have you got whats the best method already?

Here's what i do generally,

Have a NetworkManager which is not Singleton. Define a protocol with method OnSuccess,OnError. Implement this in your ViewController which initiates the network connection. Set the delegate on NetworkManager and let delegate be called when Asynchronous request is executed.

Use delegates instead of blocks as its easy to maintain.

This may not be best solution, but hopefully it gives you some pointers.


I recommend option 2 with a little bit of what you listed for option 1. In my apps I tend to have two distinct modes of operation that operate concurrently.

Automatic downloads:App essential data is downloaded and saved directly to the database. It's initiated each time the app becomes active. As each request completes an NSNotification is sent out for any visible view controllers that may need to know about the new data.

For example, if I save player data I'll send a notification like "PlayerDataUpdated". When a view controller is visible it listens for notifications. When it's not visible it doesn't listen for notifications since any changes in to the database will be discovered during viewWillAppear.

User Initiated downloads:For user-initiated network requests, such as pull to refresh, you should call the appropriate method on NetworkManager from the view controller that needs the updated data.