如何确定主线程上正在运行什么+减慢UI? [英] How to determine what's running on main thread + slowing down UI?

查看:66
本文介绍了如何确定主线程上正在运行什么+减慢UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中加入了新的数据加载功能.它旨在将大型数据库的内容从移动设备传输并处理到后端.在我在此管道中运行的函数中,函数的全部内容都在

I put in a new data loading feature in my app. It's meant to transfer and process the contents of a large database from the mobile device into the backend. In ever function I run in this pipeline, the entire contents of the function are inside a

dispatch_async

这调度到一个非主线程.我还通过日志验证了这些文件是否有效.管道中的每个函数都不在主线程上.但是我遇到UI冻结的情况.

This dispatches to a non-main thread. I have also verified with logs that these are working. Every function along the pipeline is off the main thread. And yet I am experiencing UI freeze-up.

问题:

  1. 弄清楚主线程上的内容以及它在做什么/正在等待的最佳方法是什么?
  2. 是否有可能让非主线程做太多事情而实际上影响主线程?

推荐答案

您应该使用Instruments来分析您的应用. Time Profiler(确保使用记录等待线程"选项)和系统跟踪一样有用.对于这两种方式,您可能都想使用线程策略"视图,,主要关注主线程. WWDC视频中有很多描述了各种方法的视频,包括过时但仍然相关的2012年视频

You should profile your app with Instruments. Time Profiler (make sure to use "Record Waiting Threads" option) can be useful, as is System Trace. For both, you might want to use the "Thread Strategy" view, , focusing on the main thread. There are a bunch of WWDC videos that describe various approaches, including the dated, yet still relevant, 2012 video Building Concurrent User Interfaces on iOS. Also look for newer WWDC videos that reference "profiling" and "instruments".

关于对性能造成不利影响的非主线程,通常可以忽略不计,并且您可能还会发生其他事情.唯一的重大问题是,当您使用的是不支持多线程的非常旧的设备时.

Regarding non-main threads adversely affecting performance, it's generally negligible and you probably have something else going on. The only time it's a significant problem is when you're using a very old device that doesn't support multithreading.

顺便说一句,您是否100%确定主线程确实没有响应?还是您可能只是没有及时看到UI更新?这可能是由于不小心从后台线程执行UI更新,而不是将它们分派回主队列造成的.

By the way, are you 100% sure the main thread is really not being responsive? Or is it possibly that you're just not seeing UI updates being reflected in a timely manner? This can be a result of accidentally doing UI updates from the background thread rather than dispatching them back to the main queue.

如果您需要更具体的建议,我们需要一个有关性能问题的可复制示例.但概括地说,

If you want more concrete counsel, we need a reproducible example of the performance problem. But in the abstract,

  • 确保您在主队列上没有进行任何耗时的操作;
  • 确保所有UI更新都分派回主队列...这包括可能触发UIKit控件更新的任何内容
  • 确保您的代码不会等待"主线程中的任何内容(例如,等待信号量,等待操作队列中的操作,等待调度组等);和
  • 请记住,并非所有异步API都会在后台队列上调用其完成处理程序(实际上,为了易于使用,许多异步API将其分派回主队列),因此,如果您在完成操作中花费了很多时间,处理程序,请确认它实际上是否正在后台线程上运行.
  • make sure you don't have any time consuming operations on the main queue;
  • make sure all UI updates are dispatched back to the main queue ... this includes anything that might trigger the update of a UIKit control;
  • make sure your code doesn't "wait" for anything from the main thread (e.g. waiting for a semaphore, waiting for operations on an operation queue, waiting for dispatch group, etc.); and
  • remember that not all asynchronous API will call their completion handlers on background queues (in fact, many dispatch it back to the main queue for the sake of ease of use), so if you're doing anything time consuming in a completion handler, confirm that it's actually getting run on a background thread or not.

这篇关于如何确定主线程上正在运行什么+减慢UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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