maxep / MXParallaxHeader

Simple parallax header for UIScrollView
MIT License
1.73k stars 251 forks source link

Exception 'NSRangeException' when unregistering KVO #28

Closed YanSte closed 8 years ago

YanSte commented 8 years ago

Crash on

this is my error : Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <(null) 0x0> for the key path "contentOffset" from <MXParallaxView 0x1569c8860> because it is not registered as an observer.' *\ First throw call stack: (0x180a42db0 0x1800a7f80 0x180a42cf8 0x1813590a8 0x181358b84 0x181358ac8 0x100ddaf80 0x185eb4014 0x185b994a0 0x185b9c924 0x181396cb0 0x1800c1ae8 0x1809209fc 0x1809f6bc0 0x180920c50 0x182208088 0x185c0a088 0x100281a58 0x1804be8b8) libc++abi.dylib: terminating with uncaught exception of type NSException

sunnyyoung commented 8 years ago

Same problem

YanSte commented 8 years ago

i try the try catch form but not work

maxep commented 8 years ago

Can you give a bit more context ? I cant reproduce on samples. Are you adding the parallax header to a UITableView ? I've made some changes recently that may have introduce this bug.

This does not work ? :

- (void)willMoveToSuperview:(UIView *)newSuperview {
    @try  {
        [self.superview removeObserver:self.parent forKeyPath:NSStringFromSelector(@selector(contentOffset)) context:kMXParallaxHeaderKVOContext];
    } @catch (NSException *exception) {}
}

Can you try this ? :

- (void)willMoveToSuperview:(UIView *)newSuperview {
    if (self.parent) {
        [self.superview removeObserver:self.parent forKeyPath:NSStringFromSelector(@selector(contentOffset)) context:kMXParallaxHeaderKVOContext];
    }
}

- (void)didMoveToSuperview {
    if (self.parent) {
        [self.superview addObserver:self.parent
                         forKeyPath:NSStringFromSelector(@selector(contentOffset))
                            options:NSKeyValueObservingOptionNew
                            context:kMXParallaxHeaderKVOContext];
    }
}

Thanks for the report.

silvanosouzajunior commented 8 years ago

same problem here

leolelego commented 8 years ago

@maxep the try/catch doesn't fix.

The second proposition give this :

2016-06-18 at 20 11 26

wade0595 commented 8 years ago

same problem here

leave the view

libc++abi.dylib: terminate_handler unexpectedly threw an exception

maxep commented 8 years ago

Is v0.5.0 has this issue ? are you adding a parallax header to a table view?

leolelego commented 8 years ago

@maxep yes, this is a tableview from me, with static cell. And the app crash when I pop to the view before (UInavigationBar back button).

silvanosouzajunior commented 8 years ago

on the version v0.5.0 is working well

2016-06-20 14:54 GMT-03:00 Léo Derbois notifications@github.com:

@maxep https://github.com/maxep yes, this is a tableview from me, with static cell. And the app crash when I pop to the view before (UInavigationBar back button).

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/maxep/MXParallaxHeader/issues/28#issuecomment-227218175, or mute the thread https://github.com/notifications/unsubscribe/ALLOfxCFXh9eaR6A8gmSs5ZOvseOW5ZEks5qNtPFgaJpZM4I11FO .

leolelego commented 8 years ago

@maxep I just tried like this : pod 'MXParallaxHeader','= 0.5.0' And the crash was not in this release.

developforapple commented 8 years ago
- (void)willMoveToSuperview:(UIView *)newSuperview {
    [self.superview removeObserver:self.parent forKeyPath:NSStringFromSelector(@selector(contentOffset)) context:kMXParallaxHeaderKVOContext];
}

- (void)setTableHeaderViewFrame:(CGRect)frame {

    //Create a table header view that will raise KVO
    MXParallaxView *headerView = [[MXParallaxView alloc] initWithFrame:frame];
    headerView.parent = self;

    [headerView addSubview:self.contentView];
    [(UITableView *)self.scrollView setTableHeaderView:headerView];
    [headerView setNeedsLayout];
}

Conflict between the 2 methods above.
self.contentView's class is MXParallaxView [headerView addSubview:self.contentView]; self.contentView's superView class also is MXParallaxView.

So [self.superview removeObserver:self.parent forKeyPath:NSStringFromSelector(@selector(contentOffset)) context:kMXParallaxHeaderKVOContext]; make crash.

My Code:

- (void)willMoveToSuperview:(UIView *)newSuperview {
    if ([self.superview isKindOfClass:[UIScrollView class]]) {
        [self.superview removeObserver:self.parent forKeyPath:NSStringFromSelector(@selector(contentOffset)) context:kMXParallaxHeaderKVOContext];
    }
}

- (void)didMoveToSuperview {
    if ([self.superview isKindOfClass:[UIScrollView class]]) {
        [self.superview addObserver:self.parent
                     forKeyPath:NSStringFromSelector(@selector(contentOffset))
                        options:NSKeyValueObservingOptionNew
                        context:kMXParallaxHeaderKVOContext];
    }
} 
jj-12 commented 8 years ago

Good fix, but are there any plans of getting this into the next release of MXSegmentedPager?

maxep commented 8 years ago

Fix by #31