Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix KVO crash #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 37 additions & 72 deletions LGPlusButtonsView/LGPlusButtonsView.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ typedef NS_ENUM(NSUInteger, LGPlusButtonDescriptionsPosition)
LGPlusButtonDescriptionsPositionRight = 1
};

@property (assign, nonatomic, getter=isObserversAdded) BOOL observersAdded;
@property (assign, nonatomic, getter=isObserversForScrollViewAdded) BOOL observersForScrollViewAdded;

@property (assign, nonatomic) LGPlusButtonDescriptionsPosition descriptionsPosition;

@property (assign, nonatomic) UIView *parentView;
Expand Down Expand Up @@ -300,6 +297,9 @@ + (instancetype)plusButtonsViewWithNumberOfButtons:(NSUInteger)numberOfButtons

- (void)dealloc
{
[self removeObservers:self.superview];
[self setObservedScrollView:nil];

#if DEBUG
NSLog(@"%s [Line %d]", __PRETTY_FUNCTION__, __LINE__);
#endif
Expand Down Expand Up @@ -336,10 +336,8 @@ - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
- (void)willMoveToSuperview:(UIView *)newSuperview
{
[self removeObservers:self.superview];

if (newSuperview)
[self addObservers:newSuperview];

[self addObservers:newSuperview];

[super willMoveToSuperview:newSuperview];
}

Expand Down Expand Up @@ -1907,58 +1905,42 @@ - (void)deselectPlusButtonViewWithAnimationType:(LGPlusButtonAnimationType)type

- (void)addObservers:(UIView *)view
{
if (!self.isObserversAdded && view)
{
_observersAdded = YES;

[view addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];

if ([view isKindOfClass:[UIScrollView class]])
{
[view addObserver:self forKeyPath:@"contentInset" options:NSKeyValueObservingOptionNew context:nil];
[view addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
[view addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}

if (_observedScrollView)
{
NSAssert([_observedScrollView isKindOfClass:[UIScrollView class]], @"observedScrollView needs to have UIScrollView kind of class");

_observersForScrollViewAdded = YES;
if (!view) return;

[view addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];

if ([view isKindOfClass:[UIScrollView class]])
[self setObservedScrollView:view];
}

[_observedScrollView addObserver:self forKeyPath:@"contentInset" options:NSKeyValueObservingOptionNew context:nil];
[_observedScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
[_observedScrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}
}
- (void)addScrollViewObservers:(UIScrollView *)observedScrollView
{
if (!observedScrollView) return;

NSAssert([observedScrollView isKindOfClass:[UIScrollView class]], @"observedScrollView needs to have UIScrollView kind of class");

[observedScrollView addObserver:self forKeyPath:@"contentInset" options:NSKeyValueObservingOptionNew context:nil];
[observedScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
[observedScrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}

- (void)removeObservers:(UIView *)view
{
if (self.isObserversAdded && view)
{
_observersAdded = NO;

[view removeObserver:self forKeyPath:@"frame"];

if ([view isKindOfClass:[UIScrollView class]])
{
[view removeObserver:self forKeyPath:@"contentInset"];
[view removeObserver:self forKeyPath:@"contentOffset"];
[view removeObserver:self forKeyPath:@"contentSize"];
}

if (self.isObserversForScrollViewAdded)
{
NSAssert([_observedScrollView isKindOfClass:[UIScrollView class]], @"observedScrollView needs to have UIScrollView kind of class");

_observersForScrollViewAdded = NO;
if (!view) return;

[view removeObserver:self forKeyPath:@"frame"];

if (view == self.observedScrollView)
[self setObservedScrollView:nil];
}

[_observedScrollView removeObserver:self forKeyPath:@"contentInset"];
[_observedScrollView removeObserver:self forKeyPath:@"contentOffset"];
[_observedScrollView removeObserver:self forKeyPath:@"contentSize"];
}
}
- (void)removeScrollViewObservers:(UIScrollView *)observedScrollView
{
if (!observedScrollView) return;

[observedScrollView removeObserver:self forKeyPath:@"contentInset"];
[observedScrollView removeObserver:self forKeyPath:@"contentOffset"];
[observedScrollView removeObserver:self forKeyPath:@"contentSize"];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
Expand Down Expand Up @@ -2025,25 +2007,8 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

- (void)setObservedScrollView:(UIScrollView *)observedScrollView
{
if (observedScrollView)
NSAssert([observedScrollView isKindOfClass:[UIScrollView class]], @"observedScrollView needs to have UIScrollView kind of class");

if (self.isObserversForScrollViewAdded)
{
[_observedScrollView removeObserver:self forKeyPath:@"contentInset"];
[_observedScrollView removeObserver:self forKeyPath:@"contentOffset"];
[_observedScrollView removeObserver:self forKeyPath:@"contentSize"];
}

if (observedScrollView)
{
_observersForScrollViewAdded = YES;

[observedScrollView addObserver:self forKeyPath:@"contentInset" options:NSKeyValueObservingOptionNew context:nil];
[observedScrollView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew context:nil];
[observedScrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
}
else _observersForScrollViewAdded = NO;
[self removeScrollViewObservers:self.observedScrollView];
[self addScrollViewObservers:observedScrollView];

_observedScrollView = observedScrollView;
}
Expand Down