1

I am having a problem in swift 4. In my controller I have a list and a segmented element that reload the list when it changes, and when I move fast throught the list and then select another segment the APP crash and return this backtrace:

* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x20)
    frame #0: 0x0000000180dce7e8 libobjc.A.dylib`object_isClass + 16
    frame #1: 0x00000001825383e8 Foundation`KVO_IS_RETAINING_ALL_OBSERVERS_OF_THIS_OBJECT_IF_IT_CRASHES_AN_OBSERVER_WAS_OVERRELEASED_OR_SMASHED + 68
    frame #2: 0x00000001825368ec Foundation`NSKeyValueWillChangeWithPerThreadPendingNotifications + 300
    frame #3: 0x0000000185ba1614 QuartzCore`CAAnimation_setter(CAAnimation*, unsigned int, _CAValueType, void const*) + 156
    frame #4: 0x0000000185ba11ec QuartzCore`-[CAAnimation setBeginTime:] + 32
    frame #5: 0x0000000185b7e804 QuartzCore`CA::Layer::commit_animations(CA::Transaction*, double (*)(CA::Layer*, double, void*), void (*)(CA::Layer*, CA::Render::Animation*, void*), void (*)(CA::Layer*, __CFString const*, void*), void*) + 560
    frame #6: 0x0000000185b7e2a8 QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 416
    frame #7: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #8: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #9: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #10: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #11: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #12: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #13: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #14: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #15: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #16: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #17: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #18: 0x0000000185b7e20c QuartzCore`CA::Layer::commit_if_needed(CA::Transaction*, void (*)(CA::Layer*, unsigned int, unsigned int, void*), void*) + 260
    frame #19: 0x0000000185ae31ac QuartzCore`CA::Context::commit_transaction(CA::Transaction*) + 2148
    frame #20: 0x0000000185b08eb4 QuartzCore`CA::Transaction::commit() + 540
    frame #21: 0x0000000185a5da04 QuartzCore`CA::Display::DisplayLink::dispatch_items(unsigned long long, unsigned long long, unsigned long long) + 928
    frame #22: 0x0000000181dec1cc IOKit`IODispatchCalloutFromCFMessage + 392
    frame #23: 0x0000000181b10010 CoreFoundation`__CFMachPortPerform + 188
    frame #24: 0x0000000181b2a96c CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56
    frame #25: 0x0000000181b2a070 CoreFoundation`__CFRunLoopDoSource1 + 440
    frame #26: 0x0000000181b27b44 CoreFoundation`__CFRunLoopRun + 2196
    frame #27: 0x0000000181a47fb8 CoreFoundation`CFRunLoopRunSpecific + 436
    frame #28: 0x00000001838dff84 GraphicsServices`GSEventRunModal + 100
    frame #29: 0x000000018b01c2e8 UIKit`UIApplicationMain + 208
  * frame #30: 0x00000001021b5d74 Pedidos`main at AppDelegate.swift:15
    frame #31: 0x000000018156a56c libdyld.dylib`start + 4

I have seen in this post that it can be related with the observers, but I am not know how to define the observers declaration in that way. In my controller I have defined two observers for detect the keyboard

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil) 
}

And on the viewWillDisappear

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillShow, object: nil)
    NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
fenix
  • 53
  • 6
  • This has nothing to do with KVO observation because you're not using it. You're using Notification Center Observation. – inokey Mar 15 '18 at 09:46
  • Are you sure you're not observing something inside of a list cell? – inokey Mar 15 '18 at 09:49
  • 1
    @technerd He's not yelling, it's the line from the stack that tells him there is a problem. – JeremyP Mar 15 '18 at 10:04
  • @JeremyP: Then it's fine. – technerd Mar 15 '18 at 10:05
  • Exactly, I'm not yelling. The problem here is that I have using SAP SKD for iOS, and the cells type is FUIObjectTableViewCell, instead of UITableViewCell, that have a defined structure. I'm not sure is inside this cell definition they are using observers – fenix Mar 15 '18 at 10:08
  • @inokey and yey there is the message about KVO in the stack trace... – JeremyP Mar 15 '18 at 10:09
  • @user3683466 Have you tried moving the call to `super.viewWillDisappear(animated)` to after removing the observers? – JeremyP Mar 15 '18 at 10:10
  • @user3683466 exactly what I meant. You're using SAP SDK. Just by looking at few properties in FUIObjectTableViewCell, I found this: `Subclass of UILabel, implementing onChangeHandler that is invoked on changes to the value property.` So basically every property of it has it's own observer. The problem is most likely with reuse of the cell and Key Value Observation. – inokey Mar 15 '18 at 10:25
  • Not really, I will try – fenix Mar 15 '18 at 10:25
  • You are right! The FUILabel implements observers :). I am going to remove this cell definition and use UITableViewCell instead for doing a test – fenix Mar 15 '18 at 10:32
  • @JeremyP that is true. Yet, NotificationCenter Observers and Key Value Observers are not related, that's why I suggested the issue has nothing to do with Keyboard notifications. – inokey Mar 15 '18 at 10:36
  • It works fine now!! the problem was FUIObjectTableViewCell Thanks a lot @inokey, I'm searching how to give you points but I do not know how to do it, I'm afraid that I can not – fenix Mar 15 '18 at 12:17
  • @fenix I will post my comments as answer and you can accept it as an answer – inokey Mar 16 '18 at 07:17
  • @fenix I wrote `FUIObjectTableViewCell`, can you share a repro to stan.stadelman@sap.com? :) (also, which SDK version is being used?) – Stan May 11 '18 at 18:08

1 Answers1

1

Notification Center Observation has nothing to do with Key Value Observation, so the problem in a stack trace derived from something else.

If you're using some custom classes for tableViewCells make sure they reused properly.

In your case using FUIObjectTableViewCell that has almost every property having their own call back onChangeHandler that is according to FUI documentation invoked on changes to the value property - basically on KVO.

inokey
  • 5,434
  • 4
  • 21
  • 33