简体   繁体   English

Android加载器与处理程序

[英]Android loaders vs Handlers

The loaders help in asynchronously getting the data from a data source. loaders帮助异步获取数据源中的数据。 We can achieve the same effect using Handlers , where we can kick off a thread or execute a Executor and can fetch the data in that thread. 我们可以使用Handlers实现相同的效果,我们可以启动线程或执行Executor并可以获取该线程中的数据。 Once data is fetched, we can update the UI using the UI handler Message mechanism. 获取数据后,我们可以使用UI处理程序Message机制更新UI。 Then why do go for coding the complex loaders when we can achieve the same by using Handlers . 那么为什么我们可以通过使用Handlers来实现相同的复杂加载器编码。

Loaders were introduced to make it easier in implementing correct data loading on android platform. 引入了加载程序,以便更容易在Android平台上实现正确的数据加载。 This means: 这意味着:

  • doing all the heavy stuff on background thread 在背景线程上做所有沉重的事情
  • safely introducing loaded data in UI 安全地在UI中引入加载的数据
  • caching of data, this means improving speed 缓存数据,这意味着提高速度
  • loaders can live outside the Activity lifecycle, so if you have config change then your data is not destroyed 加载器可以在Activity生命周期之外生存,因此如果您有配置更改,那么您的数据不会被销毁
  • loaders will be reloaded once your data store changes 数据存储更改后,将重新加载加载程序

using Handlers,Executors, or AsyncTasks does not take into account all above points. 使用Handler,Executors或AsyncTasks不会考虑以上所有要点。 Your will have to manage this by yourself, and this is the work android developers put into Loaders implementation. 你必须自己管理这个,这是android开发人员在Loaders实现中的工作。

Ie. IE浏览器。 using AsyncTask for loading some data requires you to watch out for screen rotations, ie. 使用AsyncTask加载某些数据需要您注意屏幕旋转,即。 you must somehow retain reference to your AsyncTask that might still do something in background once you activity is recreated due to screen rotation. 你必须以某种方式保留对AsyncTask的引用,一旦你因为屏幕旋转而重新创建活动,它仍然可以在后台执行某些操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM