Question

In a typical layered architecture, how can I effectively communicate errors originating in data access layer to a UIViewContoller?

I have following design:

UIViewControllers --> datacontroller --> specificserviceproxy --> serviceproxybase

serviceproxybase initiates calls to a web service. I check for network availability before invoking operations on the web service, and want to alert users in case of network breakdown.

What is the best practice solution? Thanks.

Was it helpful?

Solution

Follow the pattern used my much of Cocoa-touch. Many methods return nil or NO when there is an error and such methods have an NSError out parameter with details about the error.

You can propagate the result and error up the layers as needed. Or a layer may wrap the error with a more layer specific error and pass up the new error.

Example methods that follow this pattern are:

NSFileManager copyItemAtPath:toPath:error:
NSFileManager attributesOfItemAtPath:error:

OTHER TIPS

You can use NSNotificationCenter to post and receive NSNotifications for various error states.

In serviceproxybase you would post the notification for any interested observers:

[[NSNotificationCenter defaultCenter] postNotificationName:@"NoConnectionNotification" object:nil userInfo:someErrorInfoObject];

Typically you would package up an object in someErrorInfoObject that gives some additional details to the observer, such as an NSError object, the service request that failed, or a service's error code. In a view controller you would listen for that notification in viewDidLoad or viewWillAppear:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleNoConnectionError:)
                                             name:@"NoConnectionNotification"
                                           object:nil];

Now whenever a notification with the name NoConnectionNotification is posted, the view controller will receive a message to handleNoConnectionError: with the corresponding NSNotification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top