shiami
shiami

Reputation: 7264

iOS - Best practice for handling network response cross ViewControllers

For example, There are 3 sequencial view controllers, A, B, C, in the navigation stack. After sending an async network request in view controller C in the background thread, User may go back to B or A.

What is the best way to handle the network response no matter where the current view controller is?

Thanks.

Upvotes: 2

Views: 338

Answers (2)

Mohannad A. Hassan
Mohannad A. Hassan

Reputation: 1648

Since the three view controllers may be interested in the response, don't make it in any of them. Create a class, let's say NetworkHandler with method - (void) makeRequest. You can make it a singleton or add it to the AppDelegate.

In C, call this method.

When - (void) makeRequest finishes the call, make an NSNotification and make all three view controllers register to this notification.

Upvotes: 7

Sharme
Sharme

Reputation: 637

It is better to have a Singleton class. You can have some delegate also to notify the controller that response received and do the respective action.

Upvotes: 1

Related Questions