-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSObject+SKObserver.m
105 lines (86 loc) · 3.05 KB
/
NSObject+SKObserver.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// NSObject+SKObserver.m
// SKObserver
//
// Created by Sachin Kesiraju on 1/19/17.
// Copyright © 2017 Sachin Kesiraju. All rights reserved.
//
#import "NSObject+SKObserver.h"
#import <objc/runtime.h>
static void * SKObserverContext = &SKObserverContext;
static void * SKObserverAssosciatedObjectKey = &SKObserverAssosciatedObjectKey;
#pragma mark - SKObserver
@interface SKObserver : NSObject
@property (assign, nonatomic) __unsafe_unretained id observedObject;
@property (copy, nonatomic) NSString *keyPath;
@property (copy, nonatomic) SKObserverBlock observerBlock;
@end
@implementation SKObserver
- (id) initForObject:(id) object keyPath:(NSString *) keyPath block:(SKObserverBlock) block
{
NSParameterAssert(object);
NSParameterAssert(keyPath);
NSParameterAssert(block);
self = [super init];
if (self) {
_observedObject = object;
_keyPath = keyPath;
_observerBlock = block;
[_observedObject addObserver:self forKeyPath:_keyPath options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:SKObserverContext];
}
return self;
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if (context == SKObserverContext) {
_observerBlock(change);
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
@end
#pragma mark - NSObject category
@implementation NSObject (SKObserver)
- (NSMutableArray *) currentKeyPathObservers
{
@synchronized (self) {
NSMutableArray *keyPathObservers = objc_getAssociatedObject(self, SKObserverAssosciatedObjectKey);
if (!keyPathObservers) {
keyPathObservers = [NSMutableArray array];
objc_setAssociatedObject(self, SKObserverAssosciatedObjectKey, keyPathObservers, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return keyPathObservers;
}
}
- (id) sk_addObserverForKeyPath:(NSString *)keyPath withBlock:(SKObserverBlock)block
{
NSParameterAssert(block);
SKObserver *observer = [[SKObserver alloc] initForObject:self keyPath:keyPath block:block];
NSMutableArray *currentObservers = [self currentKeyPathObservers];
@synchronized (currentObservers) {
[currentObservers addObject:observer];
}
return observer;
}
- (void) sk_removeObserver:(id)observer
{
if (observer && [observer isKindOfClass:[SKObserver class]]) {
SKObserver *sk_observer = (SKObserver *) observer;
[self removeObserver:sk_observer forKeyPath:sk_observer.keyPath];
NSMutableArray *currentObservers = [self currentKeyPathObservers];
@synchronized (currentObservers) {
[currentObservers removeObjectIdenticalTo:sk_observer];
}
}
}
- (void) sk_removeAllObservers
{
NSMutableArray *currentObservers = [self currentKeyPathObservers];
@synchronized (currentObservers) {
for (SKObserver *observer in currentObservers) {
[self sk_removeObserver:observer];
}
}
}
@end