|
| 1 | +// |
| 2 | +// NSObject+SAKeyValueObserver.m |
| 3 | +// SensorsAnalyticsSDK |
| 4 | +// |
| 5 | +// Created by 陈玉国 on 2023/2/23. |
| 6 | +// Copyright © 2015-2023 Sensors Data Co., Ltd. All rights reserved. |
| 7 | +// |
| 8 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +// you may not use this file except in compliance with the License. |
| 10 | +// You may obtain a copy of the License at |
| 11 | +// |
| 12 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +// |
| 14 | +// Unless required by applicable law or agreed to in writing, software |
| 15 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +// See the License for the specific language governing permissions and |
| 18 | +// limitations under the License. |
| 19 | +// |
| 20 | + |
| 21 | +#if ! __has_feature(objc_arc) |
| 22 | +#error This file must be compiled with ARC. Either turn on ARC for the project or use -fobjc-arc flag on this file. |
| 23 | +#endif |
| 24 | + |
| 25 | +#import "NSObject+SAKeyValueObserver.h" |
| 26 | +#import <objc/runtime.h> |
| 27 | +#import "SALog.h" |
| 28 | + |
| 29 | +static void *const kSAKVOObserverInfosKey = (void *)&kSAKVOObserverInfosKey; |
| 30 | +static void *const kSAKVOTargetInfosKey = (void *)&kSAKVOTargetInfosKey; |
| 31 | +static void *const kSAKVOObserversKey = (void *)&kSAKVOObserversKey; |
| 32 | +static void *const kSAKVOTargetsKey = (void *)&kSAKVOTargetsKey; |
| 33 | + |
| 34 | +@implementation SAKVOObject |
| 35 | + |
| 36 | +- (instancetype)initWithKeyPath:(NSString *)keyPath context:(void *)context { |
| 37 | + if (self = [super init]) { |
| 38 | + _keyPath = keyPath; |
| 39 | + _context = [NSValue valueWithPointer:context]; |
| 40 | + } |
| 41 | + return self; |
| 42 | +} |
| 43 | + |
| 44 | +@end |
| 45 | + |
| 46 | +@implementation NSObject (SAKeyValueObserver) |
| 47 | + |
| 48 | +- (void)setSensorsdata_KVO_observerInfos:(NSMutableDictionary *)sensorsdata_KVO_observerInfos { |
| 49 | + objc_setAssociatedObject(self, kSAKVOObserverInfosKey, sensorsdata_KVO_observerInfos, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 50 | +} |
| 51 | + |
| 52 | +- (NSMutableDictionary *)sensorsdata_KVO_observerInfos { |
| 53 | + NSMutableDictionary *observerInfos = objc_getAssociatedObject(self, kSAKVOObserverInfosKey); |
| 54 | + if (!observerInfos) { |
| 55 | + observerInfos = [NSMutableDictionary dictionary]; |
| 56 | + self.sensorsdata_KVO_observerInfos = observerInfos; |
| 57 | + } |
| 58 | + return observerInfos; |
| 59 | +} |
| 60 | + |
| 61 | +- (void)setSensorsdata_KVO_targetInfos:(NSMutableDictionary *)sensorsdata_KVO_targetInfos { |
| 62 | + objc_setAssociatedObject(self, kSAKVOTargetInfosKey, sensorsdata_KVO_targetInfos, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 63 | +} |
| 64 | + |
| 65 | +- (NSMutableDictionary *)sensorsdata_KVO_targetInfos { |
| 66 | + NSMutableDictionary *targetInfos = objc_getAssociatedObject(self, kSAKVOTargetInfosKey); |
| 67 | + if (!targetInfos) { |
| 68 | + targetInfos = [NSMutableDictionary dictionary]; |
| 69 | + self.sensorsdata_KVO_targetInfos = targetInfos; |
| 70 | + } |
| 71 | + return targetInfos; |
| 72 | +} |
| 73 | + |
| 74 | +- (void)setSensorsdata_KVO_observers:(NSHashTable * _Nullable)sensorsdata_KVO_observers { |
| 75 | + objc_setAssociatedObject(self, kSAKVOObserversKey, sensorsdata_KVO_observers, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 76 | +} |
| 77 | + |
| 78 | +- (NSHashTable *)sensorsdata_KVO_observers { |
| 79 | + return objc_getAssociatedObject(self, kSAKVOObserversKey); |
| 80 | +} |
| 81 | + |
| 82 | +- (void)setSensorsdata_KVO_targets:(NSHashTable * _Nullable)sensorsdata_KVO_targets { |
| 83 | + objc_setAssociatedObject(self, kSAKVOTargetsKey, sensorsdata_KVO_targets, OBJC_ASSOCIATION_RETAIN_NONATOMIC); |
| 84 | +} |
| 85 | + |
| 86 | +- (NSHashTable *)sensorsdata_KVO_targets { |
| 87 | + return objc_getAssociatedObject(self, kSAKVOTargetsKey); |
| 88 | +} |
| 89 | + |
| 90 | +- (void)sensorsdata_addObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath options:(NSKeyValueObservingOptions)options context:(void *)context { |
| 91 | + if (![self shouldAddObserver:observer forKeyPath:keyPath context:context]) { |
| 92 | + return; |
| 93 | + } |
| 94 | + [self addObserver:observer forKeyPath:keyPath options:options context:context]; |
| 95 | +} |
| 96 | + |
| 97 | +- (BOOL)shouldAddObserver:(NSObject *)observer forKeyPath:(NSString *)keyPath context:(void *)context { |
| 98 | + if (!observer || !keyPath) { |
| 99 | + return NO; |
| 100 | + } |
| 101 | + NSString *observerAddress = [NSString stringWithFormat:@"%p", observer]; |
| 102 | + NSArray <SAKVOObject *>*observerInfos = self.sensorsdata_KVO_observerInfos[observerAddress]; |
| 103 | + if (!observerInfos) { |
| 104 | + observerInfos = [NSArray array]; |
| 105 | + } |
| 106 | + //filter observer infos to check if existed same observer info |
| 107 | + NSArray *existedObserverInfos = [observerInfos filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) { |
| 108 | + SAKVOObject *tempObserverInfo = evaluatedObject; |
| 109 | + NSString *tempKeyPath = tempObserverInfo.keyPath; |
| 110 | + NSValue *tempContext = tempObserverInfo.context; |
| 111 | + return [tempKeyPath isEqualToString:keyPath] && [tempContext isEqualToValue:[NSValue valueWithPointer:context]]; |
| 112 | + }]]; |
| 113 | + if (existedObserverInfos.count > 0) { |
| 114 | + return NO; |
| 115 | + } |
| 116 | + //add observer and target info |
| 117 | + SAKVOObject *observerInfo = [[SAKVOObject alloc] initWithKeyPath:keyPath context:context]; |
| 118 | + NSMutableArray <SAKVOObject *>*tempObserverInfos = [NSMutableArray arrayWithArray:observerInfos]; |
| 119 | + [tempObserverInfos addObject:observerInfo]; |
| 120 | + self.sensorsdata_KVO_observerInfos[observerAddress] = [tempObserverInfos copy]; |
| 121 | + NSString *targetAddress = [NSString stringWithFormat:@"%p", self]; |
| 122 | + NSArray <SAKVOObject *>*targetInfos = observer.sensorsdata_KVO_targetInfos[targetAddress]; |
| 123 | + NSMutableArray <SAKVOObject *>*tempTargetInfos = [NSMutableArray array]; |
| 124 | + if (targetInfos) { |
| 125 | + [tempTargetInfos addObjectsFromArray:targetInfos]; |
| 126 | + } |
| 127 | + [tempTargetInfos addObject:observerInfo]; |
| 128 | + observer.sensorsdata_KVO_targetInfos[targetAddress] = [tempTargetInfos copy]; |
| 129 | + if (!self.sensorsdata_KVO_observers) { |
| 130 | + self.sensorsdata_KVO_observers = [NSHashTable weakObjectsHashTable]; |
| 131 | + } |
| 132 | + if (!observer.sensorsdata_KVO_targets) { |
| 133 | + observer.sensorsdata_KVO_targets = [NSHashTable weakObjectsHashTable]; |
| 134 | + } |
| 135 | + [self.sensorsdata_KVO_observers addObject:observer]; |
| 136 | + [observer.sensorsdata_KVO_targets addObject:self]; |
| 137 | + |
| 138 | + return YES; |
| 139 | +} |
| 140 | + |
| 141 | +- (void)removeKVOWhenDealloc { |
| 142 | + @try { |
| 143 | + [self removeKVOTargetsWhenDealloc]; |
| 144 | + [self removeKVOObserversWhenDealloc]; |
| 145 | + } @catch (NSException *exception) { |
| 146 | + SALogError(@"remove KVO exception: %@", exception); |
| 147 | + } @finally { |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +- (void)removeKVOTargetsWhenDealloc { |
| 152 | + if (!self.sensorsdata_KVO_targets) { |
| 153 | + return; |
| 154 | + } |
| 155 | + for (NSObject *target in self.sensorsdata_KVO_targets) { |
| 156 | + NSString *targetAddress = [NSString stringWithFormat:@"%p", target]; |
| 157 | + for (SAKVOObject *targetInfo in self.sensorsdata_KVO_targetInfos[targetAddress]) { |
| 158 | + [target removeObserver:self forKeyPath:targetInfo.keyPath context:targetInfo.context.pointerValue]; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +- (void)removeKVOObserversWhenDealloc { |
| 164 | + if (!self.sensorsdata_KVO_observers) { |
| 165 | + return; |
| 166 | + } |
| 167 | + for (NSObject *observer in self.sensorsdata_KVO_observers) { |
| 168 | + NSString *observerAddress = [NSString stringWithFormat:@"%p", observer]; |
| 169 | + for (SAKVOObject *observerInfo in self.sensorsdata_KVO_observerInfos[observerAddress]) { |
| 170 | + [self removeObserver:observer forKeyPath:observerInfo.keyPath context:observerInfo.context.pointerValue]; |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +- (void)sensorsdata_dealloc { |
| 176 | + //remove observer info |
| 177 | + [self removeKVOWhenDealloc]; |
| 178 | + [self sensorsdata_dealloc]; |
| 179 | +} |
| 180 | + |
| 181 | +@end |
0 commit comments