任何方式看到谁是触发变化re:键值观察? [英] Any way to see who is triggering changes re: key value observing?

查看:197
本文介绍了任何方式看到谁是触发变化re:键值观察?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用键值观察来执行特定的动作,当某些属性改变(例如,自定义UIView的重新布局子视图,当它的框架或界限改变)。有什么办法来看看谁(即什么功能)触发这个变化?在我的 observeValueForKeyPath:ofObject:change:context:中放置一个断点没有帮助,因为我不认为我可以看到谁导致更改框架 bounds 导致KVO被触发。

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.

我不知道是谁/什么是改变我的UIView的维度。

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

编辑#1:我的堆栈跟踪的屏幕截图,当框架/边界被调用 observeValue ...

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.

推荐答案

一般来说,除了查看堆栈跟踪,你不能知道是什么导致了特定的KVC突变(因此KVO通知)。在这种情况下, - [UIView(Geometry)_applyAutoresizingMaskWithOldSuperviewSize] 似乎是罪魁祸首,但更一般来说似乎是取消存档和布局视图的过程,通知。由于此更改不是来自您的代码,因此我不知道如何知道它的来源是否有帮助。

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.

一些技巧,你可以发挥传递知识从你自己的代码更高的堆栈,你的观察方法较低的堆栈。你可以在 [[NSThread currentThread] threadDictionary] 中保存一个标志/值,然后在你的观察器方法中检查它(记住在你的高层堆栈帧之前明确删除该标志返回。)也就是说,这样做是一种skanky,除此之外,它不会给你任何信息,像这样,你的代码不是什么是直接触发的变化。

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.

这篇关于任何方式看到谁是触发变化re:键值观察?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆