Created
August 19, 2015 08:03
-
-
Save crespoxiao/35177f6af0b6220fb28b to your computer and use it in GitHub Desktop.
kvo broadcast
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
// | |
// ONEObserverStore.h | |
// OneTravel | |
// | |
// Created by xiaochengfei on 15/8/18. | |
// Copyright (c) 2015年 Didi.Inc. All rights reserved. | |
// | |
#import <ONEBaseStore/ONEBaseStore.h> | |
typedef void(^ONEObserverStoreBlock)(); | |
@interface ONEObserverStore : ONEBaseStore | |
#pragma mark - listen | |
- (void)listenToken; | |
#pragma mark - observer | |
- (void)addObserver:(id)observer responseBlock:(ONEObserverStoreBlock)responseBlock; | |
@end | |
// | |
// ONEObserverStore.m | |
// OneTravel | |
// | |
// Created by xiaochengfei on 15/8/18. | |
// Copyright (c) 2015年 Didi.Inc. All rights reserved. | |
// | |
#import "ONEObserverStore.h" | |
#import "ONEUserStore.h" | |
const NSString *ONEObserverStore_key_block = @"block"; | |
const NSString *ONEObserverStore_key_observerAddress = @"observerAddress"; | |
@interface ONEObserverStore() | |
@property(nonatomic,strong)NSMutableArray *listenedObjArray; | |
@end | |
@implementation ONEObserverStore | |
- (void)dealloc { | |
_listenedObjArray = nil; | |
} | |
- (instancetype)init { | |
self = [super init]; | |
if (self) { | |
} | |
return self; | |
} | |
#pragma mark - listen | |
- (void)listenToken { | |
[RACObserve([ONEUserStore sharedInstance], token) subscribeNext: ^(NSString *token){ | |
NSLog(@"token:%@", token); | |
for (id obj in self.listenedObjArray) { | |
if ([obj isKindOfClass:[NSDictionary class]]) { | |
NSDictionary *dic = (NSDictionary *)obj; | |
ONEObserverStoreBlock block = [dic objectForKey:ONEObserverStore_key_block]; | |
block(); | |
} | |
} | |
}]; | |
} | |
#pragma mark - observer | |
- (void)addObserver:(id)observer responseBlock:(ONEObserverStoreBlock)responseBlock { | |
NSString *observerAddress = [NSString stringWithFormat:@"%p",observer]; | |
//if alreay add observer,just return | |
__block BOOL alreadyAdd = NO; | |
[self.listenedObjArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { | |
NSDictionary *dic = (NSDictionary *)obj; | |
if ([[dic objectForKey:ONEObserverStore_key_observerAddress]isEqualToString:observerAddress]) { | |
alreadyAdd = YES; | |
*stop = YES; | |
} | |
}]; | |
if (alreadyAdd) { | |
return; | |
} | |
//add observer | |
[[ONEObserverStore sharedInstance]addObserverWithAddress:observerAddress responseBlock:responseBlock]; | |
//remove observer when observer dealloc | |
[[observer rac_willDeallocSignal]subscribeCompleted:^{ | |
[[ONEObserverStore sharedInstance]removeObserver:observerAddress]; | |
}]; | |
} | |
#pragma mark - internal method | |
- (void)addObserverWithAddress:(NSString *)observer_address responseBlock:(ONEObserverStoreBlock)responseBlock { | |
if (observer_address && responseBlock) { | |
NSDictionary *dic = @{ONEObserverStore_key_observerAddress:observer_address, | |
ONEObserverStore_key_block:responseBlock}; | |
if (![self.listenedObjArray containsObject:dic]) { | |
[self.listenedObjArray addObject:dic]; | |
} | |
} | |
} | |
- (void)removeObserver:(NSString *)observer_address { | |
__block NSInteger index = -1; | |
[self.listenedObjArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { | |
NSDictionary *dic = (NSDictionary *)obj; | |
if ([[dic objectForKey:ONEObserverStore_key_observerAddress]isEqualToString:observer_address]) { | |
index = idx; | |
*stop = YES; | |
} | |
}]; | |
if (index != -1) { | |
[self.listenedObjArray removeObjectAtIndex:index]; | |
} | |
} | |
#pragma mark - lazy load | |
- (NSMutableArray *)listenedObjArray { | |
if (!_listenedObjArray) { | |
_listenedObjArray = [NSMutableArray array]; | |
} | |
return _listenedObjArray; | |
} | |
@end | |
//////////////////////////////////////////////////////////////// | |
1.ONEBaseStore just contains some cache methods.you can replace the father class such as NSObject. | |
2.each store is a singleton,i choosed a vender lib named DOSingleton to create singleton. | |
3.ONEObserverStore listen the token value of ONEUserStore,and broadcast to all the observer. | |
4.listen,you can put it in Appdelegate or other place. | |
[[ONEObserverStore sharedInstance]listenToken]; | |
5..add observer and accpet broadcast callback. | |
@weakify(self); | |
[[ONEObserverStore sharedInstance]addObserver:self responseBlock:^{ | |
//token value changed | |
@strongify(self); | |
NSLog(@"%@",[ONEUserStore sharedInstance].token); | |
//do what you want | |
}]; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment