在主线程之外检测UI操作

编程入门 行业动态 更新时间:2024-10-09 15:19:37
本文介绍了在主线程之外检测UI操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

注意:此问题与有关从以下位置调用UIKit的警告后台线程,但没有给出以下两种方法的答案.

Note: This question is related to Warn on calls to UIKit from background threads but does not give an answer on two of the approaches below.

我的应用程序屏幕快速闪烁时出现问题.过去我已经遇到了这个问题,这是由于在主线程之外更新了UI元素.

I have a problem where the app screen blinks rapidly. I already had that problem in the past and it is due to updating the UI elements outside the main thread.

因此,我在许多地方放置了以下代码:

Therefore I've put the following code in many places:

assertMainThread();

这是:

#define assertMainThread() NSAssert([NSThread isMainThread],@"Method called using a thread other than main!")

当然,我不能用assertMainThread()覆盖整个代码,因为有很多地方,并且后台GCD队列以合法的方式使用了某些代码.

Of course I cannot cover the whole code with assertMainThread() as there are many places and some code is used in a legitimate way by background GCD queues.

我看了很多地方,但是找不到XCode或LLDB告诉我何时在主线程之外更新UI元素的方法.我以为有人可以使用符号断点或其他机制在例如主线程之外调用UIKit中的通用方法的位置处中断,但找不到方法.

I looked at many places, but could not find a way for XCode or LLDB to tell me when a UI element is updated outside the main thread. I thought that one could use symbolic breakpoints or some other mechanism to break at the place where a common method in UIKit is called outside of main thread for example, but could not find a way.

我还认为,进行此类调用时,UIKit可能会在运行时发出警告?或者至少给我们提供一些工具,以帮助您调试此类问题.

I also thought that UIKit could warn at runtime when such a call is made? Or at least give us some tools to see help debug such problems.

我看过(但没有尝试过)的另一种方法是使用一些代码覆盖技术,并尝试从视觉上提取出在代码的哪个点运行了什么线程,但是没有走这条路.

Another approach I looked (but did not try) is to use some code coverage techniques and try to extract what thread was running at what point in the code visually, but did not go that route.

您对如何解决该问题有任何想法吗?

Do you have any idea on how to tackle the problem?

推荐答案

Xcode 9及更高版本

Xcode 9引入了主线程检查器,它可以嗅探可能执行的许多相关问题主线程.您可以在Scheme选项中以及其他运行时分析器中以通常的方式启用此分析器.

Xcode 9 and later

Xcode 9 introduces a Main thread checker which sniffs for many relevant issues potentially performed out-of-main thread. You can enable this analyser the usual way in your Scheme options along with other runtime analysers.

基于 steipete 的要点–符号断点的Xcode-way解决方案:

Alternative, Xcode-way solution based on steipete's gist – symbolic breakpoint:

您可以轻松地在任何UIKit类上为其他方法添加断点,例如-[UIView setNeedsDisplay]等.

You can easily add breakpoints for some other methods on any UIKit class, like -[UIView setNeedsDisplay] etc.

更多推荐

在主线程之外检测UI操作

本文发布于:2023-11-25 06:29:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628677.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主线   操作   UI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!