ZFKVOController.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // UIScrollView+ZFPlayer.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "ZFKVOController.h"
  25. @interface ZFKVOEntry : NSObject
  26. @property (nonatomic, weak) NSObject *observer;
  27. @property (nonatomic, strong) NSString *keyPath;
  28. @end
  29. @implementation ZFKVOEntry
  30. @synthesize observer;
  31. @synthesize keyPath;
  32. @end
  33. @interface ZFKVOController ()
  34. @property (nonatomic, weak) NSObject *target;
  35. @property (nonatomic, strong) NSMutableArray *observerArray;
  36. @end
  37. @implementation ZFKVOController
  38. - (instancetype)initWithTarget:(NSObject *)target {
  39. self = [super init];
  40. if (self) {
  41. _target = target;
  42. _observerArray = [[NSMutableArray alloc] init];
  43. }
  44. return self;
  45. }
  46. - (void)safelyAddObserver:(NSObject *)observer
  47. forKeyPath:(NSString *)keyPath
  48. options:(NSKeyValueObservingOptions)options
  49. context:(void *)context {
  50. NSObject *target = _target;
  51. if (target == nil) return;
  52. BOOL removed = [self removeEntryOfObserver:observer forKeyPath:keyPath];
  53. if (removed) {
  54. // duplicated register
  55. NSLog(@"duplicated observer");
  56. }
  57. @try {
  58. [target addObserver:observer
  59. forKeyPath:keyPath
  60. options:options
  61. context:context];
  62. ZFKVOEntry *entry = [[ZFKVOEntry alloc] init];
  63. entry.observer = observer;
  64. entry.keyPath = keyPath;
  65. [_observerArray addObject:entry];
  66. } @catch (NSException *e) {
  67. NSLog(@"ZFKVO: failed to add observer for %@\n", keyPath);
  68. }
  69. }
  70. - (void)safelyRemoveObserver:(NSObject *)observer
  71. forKeyPath:(NSString *)keyPath {
  72. NSObject *target = _target;
  73. if (target == nil) return;
  74. BOOL removed = [self removeEntryOfObserver:observer forKeyPath:keyPath];
  75. if (removed) {
  76. // duplicated register
  77. NSLog(@"duplicated observer");
  78. }
  79. @try {
  80. if (removed) {
  81. [target removeObserver:observer
  82. forKeyPath:keyPath];
  83. }
  84. } @catch (NSException *e) {
  85. NSLog(@"ZFKVO: failed to remove observer for %@\n", keyPath);
  86. }
  87. }
  88. - (void)safelyRemoveAllObservers {
  89. __block NSObject *target = _target;
  90. if (target == nil) return;
  91. [_observerArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  92. ZFKVOEntry *entry = obj;
  93. if (entry == nil) return;
  94. NSObject *observer = entry.observer;
  95. if (observer == nil) return;
  96. @try {
  97. [target removeObserver:observer
  98. forKeyPath:entry.keyPath];
  99. } @catch (NSException *e) {
  100. NSLog(@"ZFKVO: failed to remove observer for %@\n", entry.keyPath);
  101. }
  102. }];
  103. [_observerArray removeAllObjects];
  104. }
  105. - (BOOL)removeEntryOfObserver:(NSObject *)observer
  106. forKeyPath:(NSString *)keyPath {
  107. __block NSInteger foundIndex = -1;
  108. [_observerArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  109. ZFKVOEntry *entry = (ZFKVOEntry *)obj;
  110. if (entry.observer == observer &&
  111. [entry.keyPath isEqualToString:keyPath]) {
  112. foundIndex = idx;
  113. *stop = YES;
  114. }
  115. }];
  116. if (foundIndex >= 0) {
  117. [_observerArray removeObjectAtIndex:foundIndex];
  118. return YES;
  119. }
  120. return NO;
  121. }
  122. @end