Skip to content

Instantly share code, notes, and snippets.

@Tricertops
Created April 24, 2014 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tricertops/11263102 to your computer and use it in GitHub Desktop.
Save Tricertops/11263102 to your computer and use it in GitHub Desktop.
NSError Recovery Attempting using Objective-Chain. Idea is, that the class, where the error occured knows how to recover from it. I'm using standard NSError recovery mechanism, which is not used in UIKit (but is used in AppKit), where the error instance holds referece to a callback object. Objective-Chain provides a reactive implementation of su…
// Network request failed, due to lost internet conenction...
OCAErrorRecoveryAttempter *recoveryAttempter = [[OCAErrorRecoveryAttempter alloc] init];
// Recovery suggestion will be be presented together with error message.
recoveryAttempter.recoverySuggestion = @"Make sure you are connected to the Internet and try again.";
// Creates reactive recovery option and chains an invocation to [self start].
[[recoveryAttempter addRecoveryOptionWithTitle:@"Retry"] invoke:OCAInvocation(self, start)];
// Creates new NSError from existing, so it can add itself as a recoveryAttempter (since errors are immutable).
self.error = [recoveryAttempter recoverableError:error];
// Meanwhile in View Controller...
// Observes request.error and when non-nil, it invokes [self presentError:error].
[[OCAProperty(self, submitRequest.error, NSError) skipNil] invoke:OCAInvocation(self, presentError: OCAPH(NSError) )];
- (void)presentError:(NSError *)error {
// Creates UIAlertView from localized descriptions of the error, adds button for each recovery option and invokes recovery callbacks when clicked.
[[UIAlertView alertViewWithError:error title:@"Network Error Occured" cancelButton:@"Cancel"] show];
}
@Tricertops
Copy link
Author

Resulting Alert View:

Result

Clicking “Retry” calls -start method of the failed request.

References

OCAErrorRecoveryAttempter as a part of larger reactive Objective-Chain framework. OCAInvocation and OCAProperty are from Objective-Chain as well.

+alertViewWithError:title:cancelButton: as a part of multi-pupose utility library with many various class additions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment