0

I'm using key value observing to perform specific actions whenever certain properties are changed (e.g., relayout subviews of a custom UIView whenever its frame or bounds change). Is there any way to see who (i.e., what function) is triggering this change? Putting a breakpoint in my observeValueForKeyPath:ofObject:change:context: doesn't help because I don't think I can see who caused the change to frame or bounds that caused KVO to be triggered.

In my specific example, I can't figure out who/what is changing the dimensions of my UIView.

EDIT #1: A screenshot of my stack trace, paused when frames/bounds is called within observeValue...

Also note that this is just for debugging purposes.

Stack Trace

Ken M. Haggerty
  • 24,902
  • 5
  • 28
  • 37
  • 1
    Are you sure? Try dragging the slider at the bottom of the stack trace window all the way to the right, you should be able to see the function that modified the property you're observing – CodaFi Mar 26 '13 at 21:50
  • I've added a screenshot of my stack trace window, so please let me know what you see. I always have a hard time reading these. – Ken M. Haggerty Mar 26 '13 at 22:01
  • Thanks @MartinR. Any idea what `autoresizeArchivedView` is and why it is called? It is not a function I have written. I turned `autoLayout` off, if that is relevant(?). – Ken M. Haggerty Mar 26 '13 at 22:13
  • Each view has autosizing options (even without auto layout). I assume that these are applied when the view is loaded from the nib file. – Martin R Mar 26 '13 at 22:32
  • Any ideas why it might have worked yesterday but not today? E.g., is there a checkbox somewhere I might have accidentally unchecked? Whenever I post to SO it always seems like this is the issue rather than anything I've written in my code. – Ken M. Haggerty Mar 26 '13 at 22:45
  • @MartinR The weird setting of my frame to height 0.0 seems to happen sometime after the object's `awakeFromNib` and before the view controller's `viewDidLoad`. What are some functions that might be called between those two times? – Ken M. Haggerty Mar 26 '13 at 22:50

1 Answers1

0

In general, no, other than looking at the stack trace, you can't know what caused a particular KVC mutation (and hence KVO notification). In this case, -[UIView(Geometry) _applyAutoresizingMaskWithOldSuperviewSize] appears to be the culprit, but more generally it appears to be the process of unarchiving and layout out the view that's triggering the notification. Since this change didn't come from your code, I'm not sure how knowing where it came from would be helpful.

There are some tricks you can play to pass "knowledge" from your own code higher up the stack to your observation method lower on the stack. You can stash a flag/value in [[NSThread currentThread] threadDictionary] and then check for it in your observer method (remembering to explicitly remove the flag before your higher up stack frame returns.) That said, doing so is kind of skanky, and on top of that, it won't give you any information about cases like this where your code isn't what's directly triggering the change.

ipmcc
  • 29,581
  • 5
  • 84
  • 147