应用程序可能在其主线程上做了太多工作 [英] The application may be doing too much work on its main thread

查看:22
本文介绍了应用程序可能在其主线程上做了太多工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Android SDK/API 环境的新手.这是我第一次尝试绘制绘图/图表.我尝试使用 3 个不同的免费库在模拟器上运行不同类型的示例代码,但布局屏幕上没有显示任何内容.logcat 正在重复以下消息:

<前>W/Trace(1378):来自 nativeGetEnabledTags 的意外值:0I/Choreographer(1378):跳过55帧!应用程序可能在其主线程上做了太多工作.

当我运行与许可库的评估副本相关的示例代码时,问题没有持续存在并且图表工作正常.

解决方案

摘自:Android 用户界面:修复跳帧

<块引用>

任何开始开发 android 应用程序的人都会在logcat 编舞(abc):跳过了xx帧!该应用程序可能是在它的主线程上做了太多的工作." 那么它实际上是做什么的意思是,您为什么要担心以及如何解决.

这意味着您的代码需要很长时间来处理和框架因为它被跳过,也许是因为一些沉重您在应用程序或数据库的核心进行的处理访问或任何其他导致线程停止一段时间的事情.

<块引用>

这里有更详细的解释:

<块引用>

Choreographer 允许应用将自己连接到 vsync,并且适当安排时间以提高性能.

Android 视图动画在内部使用 Choreographer 来实现相同的效果目的:正确计时动画并可能改进性能.

自从 Choreographer 被告知每个 vsync 事件,我可以判断Choreographer.post* api 传递的 Runnable 之一在一帧时间内没有完成,导致跳帧.

据我了解,Choreographer 只能检测跳帧.它无法说明为什么会发生这种情况.

消息应用程序可能在其主要线."可能会产生误导.

来源:Logcat 中 Choreographer 消息的含义

为什么要担心

当这个消息在android上弹出时模拟器和跳过的帧数相当小(<100)然后你可以安全地打赌模拟器很慢——这会发生几乎所有的时间.但是如果跳过的帧数很大并且在 300+ 的数量级上可能会出现一些严重的问题你的代码.与 ios 不同,Android 设备具有多种硬件和 windows 设备.RAM 和 CPU 各不相同,如果您想要所有设备上的合理性能和用户体验,那么你需要解决这个问题.当跳过帧时,用户界面很慢并且滞后,这不是理想的用户体验.

如何解决

解决此问题需要识别存在或可能会发生长时间的处理.最好的方法是做一个线程中的所有处理,无论大小来自主 UI 线程.因此,无论是从 SQLite 数据库访问数据还是做一些核心数学或简单地对数组进行排序——在一个不同的线程

现在有一个问题,您将创建一个新线程来执行这些操作,当你运行你的应用程序时,它会崩溃说只有创建视图层次结构的原始线程才能触摸它的观点".你需要知道这个事实,android 中的 UI 可以是仅由主线程或 UI 线程更改.任何其他线程尝试这样做,失败并因此错误而崩溃.你什么需要做的是在 runOnUiThread 和里面创建一个新的 Runnable这个 runnable 你应该做所有涉及 UI 的操作.找一个例子这里.

所以我们有 Thread 和 Runnable 来处理主线程之外的数据,还有什么?android中有AsyncTask可以做很长时间UI 线程上的进程.这是最有用的,当你应用程序是数据驱动的或 web api 驱动的或使用复杂的 UI就像那些使用 Canvas 构建的一样.AsyncTask 的强大之处在于允许在后台做事,一旦你做完处理,您可以简单地在 UI 上执行所需的操作,而无需造成任何滞后效应.这是可能的,因为 AsyncTask派生自 Activity 的 UI 线程——你所做的所有操作通过 AsyncTask 在 UI 上完成是与主 UI 不同的线程线程,不妨碍用户交互.

所以这是制作流畅的android所需要知道的应用程序,据我所知,每个初学者都会在他的控制台.

I am new to Android SDK/API environment. It's the first I am trying to draw a plot/chart. I tried running different kinds of sample codes on the emulator using 3 different free libraries, nothing is showing on the layout screen. The logcat is repeating the following message:

 W/Trace(1378): Unexpected value from nativeGetEnabledTags: 0
 I/Choreographer(1378): Skipped 55 frames!  The application may be doing too much work on its main thread. 

The problem didn't persist and the chart worked when I ran a sample code pertaining to an evaluation copy of a licensed library.

解决方案

taken from : Android UI : Fixing skipped frames

Anyone who begins developing android application sees this message on logcat "Choreographer(abc): Skipped xx frames! The application may be doing too much work on its main thread." So what does it actually means, why should you be concerned and how to solve it.

What this means is that your code is taking long to process and frames are being skipped because of it, It maybe because of some heavy processing that you are doing at the heart of your application or DB access or any other thing which causes the thread to stop for a while.

Here is a more detailed explanation:

Choreographer lets apps to connect themselves to the vsync, and properly time things to improve performance.

Android view animations internally uses Choreographer for the same purpose: to properly time the animations and possibly improve performance.

Since Choreographer is told about every vsync events, I can tell if one of the Runnables passed along by the Choreographer.post* apis doesnt finish in one frame’s time, causing frames to be skipped.

In my understanding Choreographer can only detect the frame skipping. It has no way of telling why this happens.

The message "The application may be doing too much work on its main thread." could be misleading.

source : Meaning of Choreographer messages in Logcat

Why you should be concerned

When this message pops up on android emulator and the number of frames skipped are fairly small (<100) then you can take a safe bet of the emulator being slow – which happens almost all the times. But if the number of frames skipped and large and in the order of 300+ then there can be some serious trouble with your code. Android devices come in a vast array of hardware unlike ios and windows devices. The RAM and CPU varies and if you want a reasonable performance and user experience on all the devices then you need to fix this thing. When frames are skipped the UI is slow and laggy, which is not a desirable user experience.

How to fix it

Fixing this requires identifying nodes where there is or possibly can happen long duration of processing. The best way is to do all the processing no matter how small or big in a thread separate from main UI thread. So be it accessing data form SQLite Database or doing some hardcore maths or simply sorting an array – Do it in a different thread

Now there is a catch here, You will create a new Thread for doing these operations and when you run your application, it will crash saying "Only the original thread that created a view hierarchy can touch its views". You need to know this fact that UI in android can be changed by the main thread or the UI thread only. Any other thread which attempts to do so, fails and crashes with this error. What you need to do is create a new Runnable inside runOnUiThread and inside this runnable you should do all the operations involving the UI. Find an example here.

So we have Thread and Runnable for processing data out of main Thread, what else? There is AsyncTask in android which enables doing long time processes on the UI thread. This is the most useful when you applications are data driven or web api driven or use complex UI’s like those build using Canvas. The power of AsyncTask is that is allows doing things in background and once you are done doing the processing, you can simply do the required actions on UI without causing any lagging effect. This is possible because the AsyncTask derives itself from Activity’s UI thread – all the operations you do on UI via AsyncTask are done is a different thread from the main UI thread, No hindrance to user interaction.

So this is what you need to know for making smooth android applications and as far I know every beginner gets this message on his console.

这篇关于应用程序可能在其主线程上做了太多工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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