当前位置: 首页 > 编程日记 > 正文

LoaderManager使用具体解释(三)---实现Loaders

这篇文字将介绍Loader<D>类,而且介绍自己定义Loader的实现。这是本系列的第三篇文章。

一:Loaders之前世界
二:了解LoaderManager
三:实现Loaders
四:实例:AppListLoader

重中之重,假设你还没有读过前面两篇文章,我建议你在深入之前先读一读那两篇文章。先简短的总结一下这篇博客覆盖了什么内容。Loader之前的世界(第一篇)描写叙述了Android3.0之前的数据加载方法和在UI主线程中运行的冗长的查询操作。这些UI非友好的API导致了应用响应变差。总总情况就是了解LoaderManager(第二篇)的写作动机。这篇文章介绍了LoaderManager类,而且讲到了它在异步加载数据中所扮演的角色。LoaderManager在Activity和Fragment的声明周期中管理Loaders,而且在配置变化时保持已加载的数据(译者注:避免了Activity重新启动导致数据重加载)。

Loader基础


Loades负责在一个单独线程中运行查询,监控数据源改变,当探測到改变时将查询到的结果集发送到注冊的监听器上(一般是LoaderManager)。以下这些特性使Loaders成为AndroidSDK中强大的工具:

1. 它封装了实际的数据加载。Activity/Fragment不再须要知道怎样加载数据。实际上,Activity/Fragment将该任务托付给了Loader,它在后台运行查询要求而且将结果返回给Activity/Fragment。

2. client不须要知道查询怎样运行。Activity/Fragment不须要操心查询怎样在独立的线程中运行,Loder会自己主动运行这些查询操作。这样的方式不仅降低了代码复杂度同事也消除了线程相关bug的潜在可能。

3. 它是为安全的事件驱动方式。Loader检測底层数据,当检測到改变时,自己主动运行新的加载获取最新数据。这使得使用Loader变得easy,client能够相信Loader将会自己自己主动更新它的数据。Activity/Fragment所须要做的就是初始化Loader,而且对不论什么反馈回来的数据进行响应。除此之外,全部其它的事情都由Loader来解决。

Loaders是一个比較高级的话题,可能须要很多其它时间来使用它。在下一节中,我们会从分析它的四个定义的特性来開始。

Loader由什么组成?


总共同拥有四个特性终于决定了一个Loader的行为:

1. 运行异步加载的任务。为了确保在一个独立线程中运行加载操作,Loader的子类必须继承AsyncTaskLoader<D>而不是Loader<D>类。AsyncTaskLoader<D>是一个抽象Loader,它提供了一个AsyncTask来做它的运行操作。当定义子类时,通过实现抽象方法loadInBackground方法来实现异步task。该方法将在一个工作线程中运行数据加载操作。

2. 在一个注冊监听器中接收加载完毕返回的结果(见附注1)。对于每一个Loader来说,LoaderManager注冊一个OnLoadCompleteListener<D>,该对象将通过调用onLoadFinished(Loader<D> loader, D result)方法使Loader将结果传送给client。Loader通过调用Loader#deliverResult(D result),将结果传递给已注冊的监听器们。

3. 三种不同状态(见附注2)。不论什么Loader将处于三种状态之中,已启动、已停止、重新启动:
a. 处于已启动状态的Loader会运行加载操作,并在不论什么时间将结果传递到监听器中。已启动的Loader将会监听数据改变,当检測到改变时运行新的加载。一旦启动,Loader将一直处在已启动状态,一直到转换到已停止和重新启动。这是唯一一种onLoadFinished永远不会调用的状态。
b. 处于已停止状态的Loader将会继续监听数据改变,可是不会将结果返回给client。在已停止状态,Loader可能被启动或者重新启动。
c. 当Loader处于重新启动状态时,将不会运行新的加载操作,也不会发送新的结果集,也不会检測数据变化。当一个Loader进入重新启动状态,它必须解除相应的数据引用,方便垃圾回收(相同地,client必须确定,在Loader无效之后,移除了全部该数据的引用)。通常,重新启动Loader不会两次调用;然而,在某些情况下他们可能会启动,所以假设必要的话,它们必须可以适时重新启动。

4. 有一个观察者接受数据源改变的通知。Loader必须实现这些Observer当中之中的一个(比方ContentObserver,BroadcastReceiver等),来检測底层数据源的改变。当检測到数据改变,观察者必须调用Loader#onContentChanged()。在该方法中运行两种不同操作:a. 假设Loader已经处于启动状态,就会运行一个新的加载操作; b. 设置一个flag标识数据源有改变,这样当Loader再次启动时,就知道应该又一次加载数据了。

到眼下为止,你应该基本知道了Loader怎样工作了。假设没有的话,我建议你先放一放,稍后再又一次读一遍(读一下这篇文档,)。也就是说,让我们从实际代码入手,写写看。

实现Loader


就如我之前陈述的那样,在实现自己定义Loader的时候有非常多须要注意。子类必须实现loadInBackground()方法,必须覆写onStartLoading(), onStoppLoading(),onReset(),onCanceled()和deliverResult(D results)来实现一个完整功能的Loader。覆写这些方法非常重要,LoaderManager将会在Activity/Fragment不同声明周期调用不同的方法。比如,当一个Activity第一次启动,它将会让LoaderManager在Activity#onStart()中启动它所拥有的每一个Loaders。假设一个Loader没有启动,LoaderManager将会调用startLoading()方法,该方法将Loader进入已启动状态而且马上调用Loader的onStartLoading()方法。也就是说,LoaderManager在后台所做的大量工作都是基于Loader正确实现的基础上,所以不要小看实现这些方法的重要性。

以下的代码就是Loader典型实现的样板。SampleLoader查询结果为一个包括SampleItem对象的列表,而且将查询结果列表List<SampleItem>返回给client:

public class SampleLoader extends AsyncTaskLoader<List<SampleItem>> {// We hold a reference to the Loader’s data here.private List<SampleItem> mData;public SampleLoader(Context ctx) {// Loaders may be used across multiple Activitys (assuming they aren't// bound to the LoaderManager), so NEVER hold a reference to the context// directly. Doing so will cause you to leak an entire Activity's context.// The superclass constructor will store a reference to the Application// Context instead, and can be retrieved with a call to getContext().super(ctx);}/****************************************************//** (1) A task that performs the asynchronous load **//****************************************************/@Overridepublic List<SampleItem> loadInBackground() {// This method is called on a background thread and should generate a// new set of data to be delivered back to the client.List<SampleItem> data = new ArrayList<SampleItem>();// TODO: Perform the query here and add the results to 'data'.return data;}/********************************************************//** (2) Deliver the results to the registered listener **//********************************************************/@Overridepublic void deliverResult(List<SampleItem> data) {if (isReset()) {// The Loader has been reset; ignore the result and invalidate the data.releaseResources(data);return;}// Hold a reference to the old data so it doesn't get garbage collected.// We must protect it until the new data has been delivered.List<SampleItem> oldData = mData;mData = data;if (isStarted()) {// If the Loader is in a started state, deliver the results to the// client. The superclass method does this for us.super.deliverResult(data);}// Invalidate the old data as we don't need it any more.if (oldData != null && oldData != data) {releaseResources(oldData);}}/*********************************************************//** (3) Implement the Loader’s state-dependent behavior **//*********************************************************/@Overrideprotected void onStartLoading() {if (mData != null) {// Deliver any previously loaded data immediately.deliverResult(mData);}// Begin monitoring the underlying data source.if (mObserver == null) {mObserver = new SampleObserver();// TODO: register the observer}if (takeContentChanged() || mData == null) {// When the observer detects a change, it should call onContentChanged()// on the Loader, which will cause the next call to takeContentChanged()// to return true. If this is ever the case (or if the current data is// null), we force a new load.forceLoad();}}@Overrideprotected void onStopLoading() {// The Loader is in a stopped state, so we should attempt to cancel the // current load (if there is one).cancelLoad();// Note that we leave the observer as is. Loaders in a stopped state// should still monitor the data source for changes so that the Loader// will know to force a new load if it is ever started again.}@Overrideprotected void onReset() {// Ensure the loader has been stopped.onStopLoading();// At this point we can release the resources associated with 'mData'.if (mData != null) {releaseResources(mData);mData = null;}// The Loader is being reset, so we should stop monitoring for changes.if (mObserver != null) {// TODO: unregister the observermObserver = null;}}@Overridepublic void onCanceled(List<SampleItem> data) {// Attempt to cancel the current asynchronous load.super.onCanceled(data);// The load has been canceled, so we should release the resources// associated with 'data'.releaseResources(data);}private void releaseResources(List<SampleItem> data) {// For a simple List, there is nothing to do. For something like a Cursor, we // would close it in this method. All resources associated with the Loader// should be released here.}/*********************************************************************//** (4) Observer which receives notifications when the data changes **//*********************************************************************/// NOTE: Implementing an observer is outside the scope of this post (this example// uses a made-up "SampleObserver" to illustrate when/where the observer should // be initialized). // The observer could be anything so long as it is able to detect content changes// and report them to the loader with a call to onContentChanged(). For example,// if you were writing a Loader which loads a list of all installed applications// on the device, the observer could be a BroadcastReceiver that listens for the// ACTION_PACKAGE_ADDED intent, and calls onContentChanged() on the particular // Loader whenever the receiver detects that a new application has been installed.// Please don’t hesitate to leave a comment if you still find this confusing! :)private SampleObserver mObserver;
}

总结


我希望本文对你实用,而且通过它能够非常好的理解Loaders和LoaderManager怎样协同工作来运行异步任务,自己主动更新查询结果。记住,Loader是你的朋友。。。假设你使用它们,你的app将从对应性能、所需代码量中收益。我希望通过把它们的细节列举出来,能够减小它的学习曲线。

附注

1. 你不须要操心为你的Loader注冊监听器,除非你不准备跟LoaderManager协同使用。LoaderManager担任的就是“listener”的角色,并将Loader返回的不论什么结果传给LoaderCallbacks#LoadFinished方法。
2. Loader也有可能处于“abandoned”状态(译者注:丢弃状态?)。这个是一个可选的中间状态,处于停止状态和重置状态之间。为了更简明的理解,再这里不讨论丢弃状态。也就是说,以我的经验来看,通常并无必要实现onAbandon()方法。

转载于:https://www.cnblogs.com/hrhguanli/p/3910872.html

相关文章:

Swift 条件编译,编译标记

1 swift 中的条件编译跟OC中的形式是相同的 #if DEBUGself.navigationView.backgroundColor Color_ff3b30;#elseself.navigationView.backgroundColor Color_main;#endif条件可以接受 os(MacOS) ,参数iOS tvOS等平台 arch(),参数为平台架构组合 arm64 ,i386 swift(),参数为版…

代码简介:向圣诞老人和他的精灵学习Google Analytics(分析)

Here are three stories we published this week that are worth your time:这是我们本周发布的三个值得您关注的故事&#xff1a; Learn Google Analytics from Santa and his elves: 12 minute read 向圣诞老人和他的精灵学习Google Analytics(分析)&#xff1a; 阅读12分钟…

生物信息大数据数据库(NCBI、EBI、UCSC、TCGA)

想系统的学习生信数据库可以先看一下北大的公开课&#xff0c;有一章专门讲的数据库与软件&#xff1a; 1-生物信息学&#xff1a;导论与方法 北大\10 生物信息数据库及软件资源 一个优秀的生信开发者能够解决如下问题&#xff1a; 如何鉴定一个重要的且没有被解决的生物学问题…

Dispatch 执行ABC任务,执行完成之后刷新UI,指定任务D

在swift中分组管理异步任务的方式 1 group enter 和 leave 进行同步管理 func method1() {// 创建一个组 &#xff0c;要是在一个控制器中去规划请求顺序&#xff0c;则这个组要是全局的组let group DispatchGroup();let queue DispatchQueue.global();//let imgsArr [&qu…

关于页游垂直同步的若干问题

这个问题要从人眼感觉抖动的原因来分析第一种情况是常说的屏幕撕裂&#xff0c;就是垂直同 步的事情&#xff0c;可以简单理解为显存的数据更新跟屏幕的绘制刷新缺少同步&#xff0c;一次屏幕刷新的结果可能是多次显存更新的片段集合&#xff0c;这种情况只能使用更接近垂直同步…

javascript函数式_JavaScript中的函数式编程—结合实际示例(第1部分)

javascript函数式by rajaraodv通过rajaraodv JavaScript中的函数式编程—结合实际示例(第1部分) (Functional Programming In JavaScript — With Practical Examples (Part 1)) Functional Programming(FP) can change the way you program for the better. But it’s hard t…

简介子窗口控件(api)

子窗口控件 壹佰软件开发小组 整理编译 回忆第七章的CHECKER程序。这些程序显示了矩形网格。当您在一个矩形中按下鼠标按键时&#xff0c;该程序就画一个x&#xff1b;如果您再按一次鼠标按键&#xff0c;那么x就消失。虽然这个程序的CHECKER1和CHECKER2版本只使用一个主窗口…

【MongoDB学习之一】初始MongoDB

环境 MongoDB4.0 win7_x64 CentOS6.5_x64 一、MongoDB简介 (1)MongoDB使用C开发。 (2)MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。 (3)MongoDB 将数据存储为一个文档。MongoDB是一个基于分布式文件存储的数据库。 (4)MongoDB使用BSON作为数据存储…

swift 和 oc中检测textfield是否输入数字

iOS 开发中用来检测输入框是否输入的是纯数字 Swift 版本 // 代理方法func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {//判断输入的是否是数字 否则无效var cs CharacterSet();// 获取除…

开源项目贡献者_如何吸引新的贡献者加入您的开源项目

开源项目贡献者by Shubheksha通过Shubheksha 如何吸引新的贡献者加入您的开源项目 (How to attract new contributors to your open source project) It’s hard to attract contributors to your FOSS project — especially contributors who are new to open source.很难吸…

滑动轮播图实现最后一张图片无缝衔接第一张图片

原理&#xff1a;使用insertBefore和insertAfter方法调整图片顺序。 测试&#xff1a;firefox/chrome/IE11正常 已知不足&#xff1a;每次播放均使用了一次insertBefore和insertAfter&#xff0c;可考虑在最后一张图的时候将前几张图片整体后移。以后有空再优化。 1、HTML结构 …

一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品(三) --高级设置一...

一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品&#xff08;三&#xff09; --高级设置一 原文:一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品&#xff08;三&#xff09; --高级设置一上一篇&#xff1a;一个完整的安装程序实例—艾泽拉斯之海…

数据结构,堆和栈和队列的概念

数据结构&#xff0c;堆和栈和队列的概念 1 什么是数据结构 数据结构是计算机存储&#xff0c;组织数据的反复改。数据结构是指相互之间存在的一种或多种特定关系的数据元素集合。 2 数据结构的逻辑结构 1 集合结构&#xff0c;元素都是孤立存在的 2 线性结构 &#xff0c;…

电子白板 矢量 编码_当涉及白板编码采访时,请记住准备

电子白板 矢量 编码by Andy Tiffany通过安迪蒂芙尼(Andy Tiffany) 当涉及白板编码采访时&#xff0c;请记住准备 (When it comes to whiteboard coding interviews, remember to PREP) PREP is a mnemonic I created to help you remember the steps involved in solving whit…

机器学习实战笔记(Python实现)-03-朴素贝叶斯

--------------------------------------------------------------------------------------- 本系列文章为《机器学习实战》学习笔记&#xff0c;内容整理自书本&#xff0c;网络以及自己的理解&#xff0c;如有错误欢迎指正。 源码在Python3.5上测试均通过&#xff0c;代码及…

SQLite.swift的简单使用

使用cocoapod 来进行引入 pod ‘SQLite.swift’ // // SQLiteTool.swift // CreateLectureForSwift // // Created by coder on 2019/6/25. // Copyright © 2019 AlexanderYeah. All rights reserved. // import Foundation import SQLite // id let id Expression(“…

Cypress USB开发文档列表(积累中)

CyUSB.chm(pdf) \Cypress\Cypress Suite USB 3.4.7\Driver Cypress CyUSB.sys Programmers Reference 内容: CyUsb.sys、CyUsb.inf 驱动程序介绍&#xff0c;如何绑定设备到驱动程序&#xff0c;以IOCTL Interface、CYIOCTL.Hd的解释为主要内容的编程指导&#xff0c;主…

对象冒充_使用您的精神探照灯进行冒充冒名顶替综合症

对象冒充by Jaime J. Rios由Jaime J. Rios “Stop that imposter! Seize them!”“停止冒名顶替者&#xff01; 抓住他们&#xff01;” I first spotted my imposter two years ago. It happened when I began learning how to code.两年前&#xff0c;我第一次发现了冒名顶…

grep 函数

linux grep命令1.作用Linux系统中grep命令是一种强大的文本搜索工具&#xff0c;它能使用正则表达式搜索文本&#xff0c;并把匹 配的行打印出来。grep全称是Global Regular Expression Print&#xff0c;表示全局正则表达式版本&#xff0c;它的使用权限是所有用户。 2.格式gr…

iOS weak 自动置为nil的实现

1 weak 自动置为nil的实现 runtime 维护了一个Weak表&#xff0c;weak_table_t 用于存储指向某一个对象的所有Weak指针。Weak表其实是一个哈希表&#xff0c; key是所指对象的地址&#xff0c;value是weak指针的地址的数组。 在对象回收的时候&#xff0c;就会在weak表中进…

iOS 缓存策略

Github https://github.com/gaosboy/kache https://github.com/swtlovewtt/WTRequestCenter https://github.com/hans007/CacheFile Image https://github.com/youger/UIImageView-ASIImageCache转载于:https://www.cnblogs.com/hl666/p/3931182.html

数据结构的简要介绍:图形如何工作

by Michael Olorunnisola通过Michael Olorunnisola 数据结构的简要介绍&#xff1a;图形如何工作 (A Gentle Introduction to Data Structures: How Graphs Work) So who wants to work at Google, Facebook, or maybe LinkedIn? Beyond their grueling interview process, o…

Catel(翻译)-为什么选择Catel

1. 介绍 这篇文章主要是为了说明&#xff0c;我们为什么要使用Catel框架作为开发WPF&#xff0c;Silverlight,和Windows phone7应用程序的开发框架。 2. 通用功能 2.1. 这是你的选择 针对需对开发者&#xff0c;再使用架构的时候是希望有很大的自由度的&#xff0c;但是大部…

iOS 三种类型的Block

Block 的copy 操作 Block 其实来讲有三种类型 全局块 NSConcreteGlobalBlock 栈块 NSConcreteStackBlock 堆块 NSConcreteMallocBlock 全局块存储在全局内存中&#xff0c;相当于单例 栈块存于栈内存中&#xff0c;超出其作用域则马上进行销毁 堆块存在于堆内存中&#x…

2.4G高频PCB天线设计

2.4G高频PCB天线设计转载于:https://www.cnblogs.com/LittleTiger/p/6215262.html

如何一起破解图形化Python调试器

15分钟内从零调试 (Zero-to-Debugging in 15 mins) You don’t realize the value of a debugger until you’re stuck working on a hard-to-visualize problem. But once you fire up a development environment with decent debugging capabilities, you’ll never look bac…

python 之路,Day11 (下)- sqlalchemy ORM

python 之路&#xff0c;Day11 - sqlalchemy ORM 本节内容 ORM介绍sqlalchemy安装sqlalchemy基本使用多外键关联多对多关系表结构设计作业1. ORM介绍 orm英文全称object relational mapping,就是对象映射关系程序&#xff0c;简单来说我们类似python这种面向对象的程序来说一切…

iOS事件响应链

1 如下 NSObject 显然是基类&#xff0c;都是继承与UIResponder. 可以看出UIApplication&#xff0c;UIView&#xff0c;UIViewController都是继承自UIResponder类&#xff0c;可以响应和处理事件 我们都是通过UIResonder 来查找控件的父视图控件。’ 发生触摸事件之后&…

论5级流水32bit risc cpu设计

前段时间用verilog写了一个32bit的risc cpu,五级流水&#xff0c;下板调试已经完全可用&#xff0c;准备后期加入浮点运算器&#xff0c;因为最近事情超级多&#xff0c;因此暂时先把RTL图传上来供大家参考&#xff0c;后面我会讲具体怎么设计。希望大家多多关注 :)转载于:http…

开源项目贡献者_嘿新手开源贡献者:请写博客。

开源项目贡献者by Shubheksha通过Shubheksha 嘿新手开源贡献者&#xff1a;请写博客。 (Hey newbie open source contributors: please blog more.) As a newbie open source contributor, I often felt lost and dejected. I couldn’t figure out how different modules fit…