Search code examples
observersios-charts

Observer not removed when leaving Charts VC with iOS-Charts 2.2.3?


I am working on adding some graphs to my app and had a working version with iOS-Charts 2.1.4. After updating to iOS-Charts 2.2.3 I get the following error when going back from the View Controller containing my CombinedChartView:

*** Terminating app due to uncaught exception     'NSInternalInconsistencyException', reason: 'An instance 0x168c5a00 of class Charts.CombinedChartView was deallocated while key value observers were still registered with it. Current observation info: <NSKeyValueObservationInfo 0x171366c0> (
<NSKeyValueObservance 0x15d0dda0: Observer: 0x168c5a00, Key path: bounds, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x15d1b090>
<NSKeyValueObservance 0x15d17a10: Observer: 0x168c5a00, Key path: frame, Options: <New: YES, Old: NO, Prior: NO> Context: 0x0, Property: 0x15db49b0>)'
*** First throw call stack:
(0x217b62eb 0x20f82dff 0x217b6231 0x21f60095 0x20f9d3cd 0x216c9921 0x217774c7 0x216c9bb9 0x216c99ad 0x22943af9 0x259b5fb5 0xf7ed1 0x2137c873)
libc++abi.dylib: terminating with uncaught exception of type NSException

Note: I do not voluntarily register any observers in this VC.

Anybody seeing the same with latest version of iOS-Charts? Great graph library by the way!

Any idea what I could be doing wrong?


Solution

  • I found the solution thanks to @Wingzero's suggestions!

    So my issue was that the ChartViewBase.init() method was called twice, thus registering the observers twice but deinit() was only called once so there was a duplicate set of observers left hanging.

    The reason for the double init() call is that I started working with the iOS-Charts lib on v.2.1 and at that time needed to manually initialise the CombinedChartView in my VC's viewDidLoad() (or so I thought). With the update to iOS-Charts 2.2.3 (or Swift 2?) this is not needed anymore and will lead to double initialisation and thus one instance of the ChartViews left hanging.

    Hope this might help someone else following the same 2.1 -> 2.2.3 update path for iOS-Charts. I've got to say, I highly recommend the iOS-Charts as a graph library for iOS. This was just my error.

    Also note that cocoaPod doesn't know about the 2.2 version of the lib yet.