random crash on pickerview didSelect : __cfrunloop_is_calling_out_to_a_source1_perform1_function

时光毁灭记忆、已成空白 提交于 2019-12-02 09:27:02

I suspect the problem is in the call to ==.

if pickerView == assignedTo

This isn't really what you mean. This forces a call to the == function and tries to evaluate equality between these two objects. If one of them is nil that will crash.

pickerView should never be nil, but I've encountered cases where UIKit sends nil for parameters that should never be nil. (This especially can happen if there are any calls to UIKit methods on background threads. Be very careful about not doing that, but it can also happen due to UIKit bugs.)

assignedTo can be nil if the view hasn't loaded. In theory you should never get this call in that case, but again, it's possible in the presence of bugs, again most commonly due to calling UIKit methods off the main thread (any UIKit methods; it doesn't always have to be related to this particular picker view), but UIKit sometimes has bugs of its own.

In any case, you don't mean "is pickerView equal to assignedTo." You mean "is pickerView exactly the same thing as assignedTo." That's ===:

if pickerView === assignedTo

=== does a pointer comparison, so even in the face of an invalid nil it's safe.

Keep in mind that this is almost certainly related to a race condition. The fact that it doesn't easily reproduce when you run it in the debugger doesn't mean anything. It might happen once in 10k tries, only in Release, exclusively on iPhone 6. That's the nature of race conditions.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!