eggswift / pull-to-refresh

#Busy Re-Building....# An easy way to use pull to refresh and infinite scrolling in Swift. Pod 'ESPullToRefresh'
MIT License
1.83k stars 253 forks source link

Remove Observer crashes #25

Closed shobhz closed 7 years ago

shobhz commented 8 years ago

removeObserver crashed in ESRefreshComponent - not always but often!

*\ Terminating app due to uncaught exception 'NSRangeException', reason: 'Cannot remove an observer <App.ESRefreshHeaderView 0x108b05430> for the key path "contentOffset" from <App.UITableView 0x1030ec200> because it is not registered as an observer.'

oonoo commented 8 years ago

Also happens in my project!

oonoo commented 8 years ago

Can you make a try-catch around the part where the KVO observer gets removed like this (sorry for objective-c code):

Safe Unsubscribe with @try / @catch

Perhaps the most pronounced annoyance with KVO is how it gets you at the end. If you make a call to –removeObserver:forKeyPath:context: when the object is not registered as an observer (whether because it was already unregistered or not registered in the first place), an exception is thrown. The kicker is that there’s no built-in way to even check if an object is registered!

Which causes one to rely on a rather unfortunate cudgel @try with an unhandled @catch:


- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
          @try {
              [object removeObserver:self forKeyPath:NSStringFromSelector(@selector(isFinished))];
          }
          @catch (NSException * __unused exception) {}
        }
}
eggswift commented 8 years ago

Good job!

eggswift commented 7 years ago

@oonoo It's welcome for you to commit pull request. Let's fix it! : )

jgavris commented 7 years ago

I've fixed this ^