Last active
February 3, 2016 12:42
-
-
Save showsky/d3038bb6556f83f0312a to your computer and use it in GitHub Desktop.
多重 delegate,有別於一般的一對一 delegate,使用 weak 避免 reference cycle。大家都不熟悉所以採用 NSNotifications
This file contains hidden or 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
| @protocol ServiceDelegate <NSObject> | |
| @optional | |
| - (void)generalService:(GeneralService *)service didRetrieveEntries:(NSArray *)entries; | |
| @end | |
| @interface GeneralService : NSObject | |
| - (void)registerDelegate:(id<ServiceDelegate>)delegate; | |
| - (void)deregisterDelegate:(id<ServiceDelegate>)delegate; | |
| @end |
This file contains hidden or 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
| @interface GeneralService () | |
| @property (nonatomic, strong) NSMutableSet *delegates; | |
| @end | |
| @implementation ZOCGeneralService | |
| - (void)registerDelegate:(id<ServiceDelegate>)delegate { | |
| if ([delegate conformsToProtocol:@protocol(ServiceDelegate)]) { | |
| [self.delegates addObject:[[WeakObject alloc] initWithObject:delegate]]; | |
| } | |
| } | |
| - (void)deregisterDelegate:(id<ServiceDelegate>)delegate { | |
| if ([delegate conformsToProtocol:@protocol(ServiceDelegate)]) { | |
| [self.delegates removeObject:[[WeakObject alloc] initWithObject:delegate]]; | |
| } | |
| } | |
| - (void)_notifyDelegates { | |
| ... | |
| for (WeakObject *object in self.delegates) { | |
| if (object.object) { | |
| if ([object.object respondsToSelector:@selector(generalService:didRetrieveEntries:)]) { | |
| [object.object generalService:self didRetrieveEntries:entries]; | |
| } | |
| } | |
| } | |
| } | |
| @end |
This file contains hidden or 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
| @interfact WeakObject : NSObject | |
| @property (nonatomic, readonly, weak) id object; | |
| + (instancetype)weakObjectWithObject:(id)object; | |
| - (instancetype)initWithObject:(id)object; | |
| @end |
This file contains hidden or 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
| @interface ZOCWeakObject () | |
| @property (nonatomic, weak) id object; | |
| @end | |
| @implementation ZOCWeakObject | |
| + (instancetype)weakObjectWithObject:(id)object { | |
| return [[[self class] alloc] initWithObject:object]; | |
| } | |
| - (instancetype)initWithObject:(id)object { | |
| if ((self = [super init])) { | |
| _object = object; | |
| } | |
| return self; | |
| } | |
| - (BOOL)isEqual:(id)object { | |
| if (self == object) { | |
| return YES; | |
| } | |
| if (![object isKindOfClass:[object class]]) { | |
| return NO; | |
| } | |
| return [self isEqualToWeakObject:(WeakObject *)object]; | |
| } | |
| - (BOOL)isEqualToWeakObject:(WeakObject *)object { | |
| if (!object) { | |
| return NO; | |
| } | |
| BOOL objectsMatch = [self.object isEqual:object.object]; | |
| return objectsMatch; | |
| } | |
| - (NSUInteger)hash { | |
| return [self.object hash]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment