是使用runOnUiThread内AsyncTask的低效和坏的? [英] Is using runOnUiThread inside AsyncTask inefficient and bad?

查看:176
本文介绍了是使用runOnUiThread内AsyncTask的低效和坏的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这听起​​来很疯狂,有人正在使用runOnUiThread里面的AsyncTask。不知何故,这是为我工作,但我想知道这是否是一个可以接受的和可靠的方法还是不行。下面是这种情况:

I know it sounds crazy that someone is using runOnUiThread inside AsyncTask. Somehow, it is working for me but I wanna know if it is an acceptable and robust approach or not. Here is the scenario:

我有一个应用程序在成功登录后,用户被渲染到下一个屏幕。在这个新的屏幕,3种不同的方法被从网络服务器加载不同类型的数据。这些方法是:

I have an app in which after successful login, user is rendered to next screen. In this new screen, 3 different methods are loading different types of data from a web server. These methods are:

  • getMembersForList():它加载的社区成员的名单,并显示在一个列表视图

  • getMembersForList() : It loads the list of community members and shows it in a listview.

getProfileData():它加载的登录用户的配置文件,并显示了他的名字,形象等在屏幕上

getProfileData() : It loads the profile of logged in user and shows his name , image etc on the screen.

我申请了3种不同的方法吧:

I applied 3 different approaches for it :

(1)调用所有3种方法只需在的onCreate即没有独占线程正在使用的任何方法。在这种情况下,从登录屏幕到该屏幕的过渡变得非常慢和黑屏显示出来了一段时间在此之前的活性显示出来。

(1) Calling all 3 methods simply in onCreate i.e. no exclusive thread is being used for any of the methods . In this case , the transition from login screen to this screen becomes very slow and black screen shows up for some time before this activity shows up.

UI线程和其他2种方法独占线程上的(2)调用getMembersForList()。在这种情况下的过渡变得快捷列表很快显示出来,但通知计数和用户名等,因为WrongThreadException出现显示不出来说这个线程不能碰其他线程的意见(TextViews用户名,通知数等,这些全局声明)。当我开始这些线程从AsyncTask的还有同样的事情发生。

(2) Calling getMembersForList() on UI thread and the other 2 methods on exclusive threads. In this case transition becomes fast and list shows up quickly but Notification counts and username etc. don't show up because WrongThreadException occurs saying that this thread can't touch other thread's views (TextViews for username, notification count etc. which are declared globally) . The same thing happens when I start these threads from an AsyncTask as well.

UI线程上的(3)调用getMembersForList(),然后开始在其中其它2方法被称为在runOnUiThread内部doInBackground()方法的AsyncTask的。这解决了上述两个问题。现在画面转换更快,WrongThread异常也没有存在的。

(3) Calling getMembersForList() on UI thread and then starting an AsyncTask in which the other 2 methods are being called in "runOnUiThread" inside doInBackground() method. This solves both the above issues. Now the screen transition is faster and the WrongThread exception is also not occuring.

目前在 approach-(3)正在为我好,但我不知道这是正确的方式做到这一点,因为runOnUiThread和AsyncTask的2个完全相反的东西。任何人都可以请清楚我怀疑这种情况。感谢名单提前。

So far the approach-(3) is working good for me but I am not sure if this is the right way to do it because runOnUiThread and AsyncTask are 2 completely opposite things. Can anyone please clear my doubts about this scenario. Thanx in advance.

推荐答案

是的,使用情况是这样的,为什么 runOnUiThread()方法存在于一个大原因第一名。我们的想法是,你让你的后台线程(S)/ 的AsyncTask 实例(S)在后台运行冗长的操作,然后提供一个简单的挂钩,他们可以用它来更新界面,当他们有的结果(或以任意间隔,作为不同块的结果变得可用)。

Yes, use-cases like this are a big reason why the runOnUiThread() method exists in the first place. The idea is you allow your background thread(s)/AsyncTask instance(s) to run your lengthy operations in the background, and then provide a simple hook that they can use to update the interface when they have the result (or at arbitrary intervals, as different pieces of the result become available).

只要这就是你在做什么,那么你的用法是好的。要避免做什么是由后台线程传递一些冗长的操作在主线程中执行冗长的操作,直接或间接的影响。

As long as that's what you're doing, then your usage is fine. What you want to avoid doing is performing a lengthy operation on the main thread, either directly or indirectly by passing in some lengthy operation from a background thread.

当然,你不必这样做的,如果你不想。你可以使用 postExecute()代替。或者你可以存储一些结果,然后使用任何类型的信息传递API来通知主线程的结果是准备好了,等等。

Of course you don't have to do it that way if you don't want to. You could use postExecute() instead. Or you could store the result somewhere and then use any sort of message-passing API to notify the main thread that the result is ready, and so on.

这篇关于是使用runOnUiThread内AsyncTask的低效和坏的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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