Created
April 24, 2014 17:41
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Resulting Alert View:
Clicking “Retry” calls
-start
method of the failed request.References
OCAErrorRecoveryAttempter
as a part of larger reactive Objective-Chain framework.OCAInvocation
andOCAProperty
are from Objective-Chain as well.+alertViewWithError:title:cancelButton:
as a part of multi-pupose utility library with many various class additions.