Created
August 29, 2014 05:58
-
-
Save quellish/604677978091703256f4 to your computer and use it in GitHub Desktop.
UIResponder Informal Protocol
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
/** | |
Informal protocol that traverses the responder chain until a concrete class provides | |
an instance of NSManagedObjectContext. If no responder provides on, UIApplication is extended to forward | |
the request to it's delegate. We extend the UIApplicationDelegate to add this new behavior. | |
Concrete responders can provide optional implementations of managedObjectContext. For example, a responder such as | |
a view may call [self managedObjectContext], which would start walking up the responder chain. When it gets to the view controller that owns the view, the view controller may have a concrete implementation of managedObjectContext. This can be a property, or a method that creates a new context, etc. The instance vended by the view controller method is what the view that called [self managedObjectContext] gets as a response from that method. | |
Both the responder chain and informal protocols are incredible powerful concepts that are often under utilized by developers outside of Apple. | |
https://developer.apple.com/library/mac/documentation/general/conceptual/devpedia-cocoacore/Protocol.html | |
https://developer.apple.com/library/ios/documentation/general/conceptual/Devpedia-CocoaApp/Responder.html | |
https://developer.apple.com/library/ios/documentation/EventHandling/Conceptual/EventHandlingiPhoneOS/event_delivery_responder_chain/event_delivery_responder_chain.html#//apple_ref/doc/uid/TP40009541-CH4-SW3 | |
**/ | |
@implementation UIResponder (CoreData) | |
- (NSManagedObjectContext *) managedObjectContext { | |
NSManagedObjectContext *result = nil; | |
// Move up the responder chain. | |
if ([self nextResponder] != nil){ | |
if ([[self nextResponder] respondsToSelector:@selector(managedObjectContext)]){ | |
result = [[self nextResponder] managedObjectContext]; | |
} | |
} | |
return result; | |
} | |
@end | |
@implementation UIApplication (CoreData) | |
- (NSManagedObjectContext *) managedObjectContext { | |
NSManagedObjectContext *result = nil; | |
// If we have gotten this far, forward to the delegate. | |
if ([[self delegate] respondsToSelector:@selector(managedObjectContext)]){ | |
result = [(id)[self delegate] managedObjectContext]; | |
} | |
return result; | |
} | |
@end | |
@protocol UIApplicationDelegateCoreData <UIApplicationDelegate> | |
// Extend UIApplicationDelegate to add the new behavior. | |
@optional | |
- (NSManagedObjectContext *) managedObjectContext; | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment