Skip to content

Commit 768ed66

Browse files
committed
Added safety checks when removing editorMode KVO observers (#71)
1 parent 330edd3 commit 768ed66

File tree

2 files changed

+45
-22
lines changed

2 files changed

+45
-22
lines changed

SCXcodeMinimap/IDEEditorArea+SCXcodeMinimap.m

+35-13
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111

1212
static void *IDEEditorAreaEditorModeObservingContext = &IDEEditorAreaEditorModeObservingContext;
1313

14+
@interface IDEEditorArea (SCXcodeMinimap_Private)
15+
16+
@property (nonatomic, assign) BOOL observersInstalled;
17+
18+
@end
19+
1420
@implementation IDEEditorArea (SCXcodeMinimap)
1521

1622
+ (void)load
@@ -19,26 +25,22 @@ + (void)load
1925
sc_swizzleInstanceMethod([self class], @selector(viewWillUninstall), @selector(sc_viewWillUninstall));
2026
}
2127

22-
- (id<IDEEditorAreaMinimapDelegate>)minimapDelegate
23-
{
24-
return objc_getAssociatedObject(self, @selector(minimapDelegate));
25-
}
26-
27-
- (void)setMinimapDelegate:(id<IDEEditorAreaMinimapDelegate>)minimapDelegate
28-
{
29-
objc_setAssociatedObject(self, @selector(minimapDelegate), minimapDelegate, OBJC_ASSOCIATION_ASSIGN);
30-
}
31-
3228
- (void)sc_viewDidInstall
3329
{
34-
[self addObserver:self forKeyPath:@"editorMode" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:IDEEditorAreaEditorModeObservingContext];
35-
30+
if(!self.observersInstalled) {
31+
[self addObserver:self forKeyPath:@"editorMode" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:IDEEditorAreaEditorModeObservingContext];
32+
[self setObserversInstalled:YES];
33+
}
34+
3635
[self sc_viewDidInstall];
3736
}
3837

3938
- (void)sc_viewWillUninstall
4039
{
41-
[self removeObserver:self forKeyPath:@"editorMode"];
40+
if(self.observersInstalled) {
41+
[self removeObserver:self forKeyPath:@"editorMode"];
42+
[self setObserversInstalled:NO];
43+
}
4244

4345
[self sc_viewWillUninstall];
4446
}
@@ -52,4 +54,24 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
5254
}
5355
}
5456

57+
- (BOOL)observersInstalled
58+
{
59+
return [objc_getAssociatedObject(self, @selector(observersInstalled)) boolValue];
60+
}
61+
62+
- (void)setObserversInstalled:(BOOL)observersInstalled
63+
{
64+
objc_setAssociatedObject(self, @selector(observersInstalled), @(observersInstalled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
65+
}
66+
67+
- (id<IDEEditorAreaMinimapDelegate>)minimapDelegate
68+
{
69+
return objc_getAssociatedObject(self, @selector(minimapDelegate));
70+
}
71+
72+
- (void)setMinimapDelegate:(id<IDEEditorAreaMinimapDelegate>)minimapDelegate
73+
{
74+
objc_setAssociatedObject(self, @selector(minimapDelegate), minimapDelegate, OBJC_ASSOCIATION_ASSIGN);
75+
}
76+
5577
@end

SCXcodeMinimap/IDEIssueAnnotationProvider+SCXcodeMinimap.m

+10-9
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@interface IDEIssueAnnotationProvider (SCXcodeMinimap_Private)
1515

16-
@property (nonatomic, assign) BOOL isObservingAnnotations;
16+
@property (nonatomic, assign) BOOL observersInstalled;
1717

1818
@end
1919

@@ -30,9 +30,10 @@ - (void)sc_providerWillUninstall
3030
{
3131
[self sc_providerWillUninstall];
3232

33-
if(self.isObservingAnnotations) {
34-
[self removeObserver:self forKeyPath:@"annotations"];
35-
}
33+
if(self.observersInstalled) {
34+
[self removeObserver:self forKeyPath:@"annotations"];
35+
[self setObserversInstalled:NO];
36+
}
3637
}
3738

3839
- (void)_didDeleteOrReplaceParagraphForAnnotation:(id)annotation
@@ -54,8 +55,8 @@ - (void)setMinimapDelegate:(id<IDEIssueAnnotationProviderDelegate>)minimapDelega
5455
objc_setAssociatedObject(self, @selector(minimapDelegate), minimapDelegate, OBJC_ASSOCIATION_ASSIGN);
5556

5657
if(minimapDelegate) {
57-
self.isObservingAnnotations = YES;
5858
[self addObserver:self forKeyPath:@"annotations" options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew context:IDEIssueAnnotationProviderIssuesObservingContext];
59+
[self setObserversInstalled:YES];
5960
}
6061
}
6162

@@ -68,14 +69,14 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
6869
}
6970
}
7071

71-
- (BOOL)isObservingAnnotations
72+
- (BOOL)observersInstalled
7273
{
73-
return [objc_getAssociatedObject(self, @selector(isObservingAnnotations)) boolValue];
74+
return [objc_getAssociatedObject(self, @selector(observersInstalled)) boolValue];
7475
}
7576

76-
- (void)setIsObservingAnnotations:(BOOL)isObservingAnnotations
77+
- (void)setObserversInstalled:(BOOL)observersInstalled
7778
{
78-
objc_setAssociatedObject(self, @selector(isObservingAnnotations), @(isObservingAnnotations), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
79+
objc_setAssociatedObject(self, @selector(observersInstalled), @(observersInstalled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
7980
}
8081

8182
@end

0 commit comments

Comments
 (0)