美文网首页
Android-Architecture学习篇二:Mvp + L

Android-Architecture学习篇二:Mvp + L

作者: 简单的救赎 | 来源:发表于2016-11-24 10:16 被阅读89次

    1 前言

    Mvp模式学习之TODO-MVP-Loaders

    It is based on theTODO-MVPsample and uses Loaders to get the data from the tasks repository.

    基于Mvp模式基础TODO-MVP,使用Loaders用任务请求获取数据

    2 基本结构

    The advantages of Loaders, from theLoaders documentation page, are:

    从Loaders文档上可以看出,Loaders的优点是:

    They provide asynchronous loading of data, removing the need for callbacks in the repository.

    Loaders提供异步加载数据,移除库repository中需要的请求回调。

    They monitor the source of their data and deliver new results when the content changes, in our case, the repository.

    Loaders会监视数据来源,根据数据内容的变化,提供最新的的结果。

    They automatically reconnect to the last loader when being recreated after a configuration change.

    当Loaders被重新改变配置后,会自动重新连接到最后一个加载程序。

    3 Loaders实现目录结构

    4 Loaders实现分析

    The Loaders (TaskLoaderandTasksLoader) are responsible for fetching the data and extend AsyncTaskLoader.

    TaskLoader和TasksLoader都是继承AsyncTaskLoader,负责取数据。

    In src/data/source/TasksLoader.java:

    @Override

    publicListloadInBackground() {

          return mRepository.getTasks();

    }

    The results are received in the UI Thread, handled by the presenter.

    在UI线程接收结果,用Presenter处理。

    InTasksPresenter.java

    Override

    publicvoidonLoadFinished(Loader>loader,Listdata) {   

        mTasksView.setLoadingIndicator(false);  

        mCurrentTasks=data;if(mCurrentTasks==null) {       

          mTasksView.showLoadingTasksError(); 

        }else{       

           showFilteredTasks();  

       }

    }

    The presenter also triggers the loading of data, like in the MVP sample but in this case it does it through the LoaderManager:

    Presenter也使用加载数据,就像在MVP例子里一样,但是它要通过LoaderManager来实现。

    @Override

    publicvoidstart() {   

        mLoaderManager.initLoader(TASKS_QUERY,null,this);

    }

    5 内容观察 Content observer 

    After every content change in the repository,notifyContentObserver()is called.

    当库中的内容变化的时候,notifyContentObserver()i会被调用。

    In src/data/source/TasksRepository.java:   

    @Override  

    publicvoiddeleteTask(@NonNull StringtaskId) {

        mTasksRemoteDataSource.deleteTask(checkNotNull(taskId)); 

        mTasksLocalDataSource.deleteTask(checkNotNull(taskId)); 

        mCachedTasks.remove(taskId);

        // Update the UI

        notifyContentObserver();

    }

    This notifies the Loader which in this case simply forces a reload of data.

    这个通知Loader,Loader在这种情况下,是一个简单的强制加载数据的Loader

    In TasksLoader.java:

    @Override

    public void onTasksChanged() {

       if(isStarted()) { 

           forceLoad();  

        }

    }

    6 类的解释

    Task:Immutable model class for a Task. 不可改变的Task模型

    TasksDbHelper:Task数据帮助类

    TasksLocalDataSource:Concrete implementation of a data source as a db. 作为数据库的数据源的具体实现。

    TasksPersistenceContract:The contract used for the db to save the tasks locally. 用于保存tasks保存到本地数据库的Contract

    TasksRemoteDataSource:Implementation of the data source that adds a latency simulating network.添加一个延迟模拟网络的数据源的实现。

    TaskLoader:Custom {@link android.content.Loader} for a {@link Task}, using the {@link TasksRepository} as its source. This Loader is a {@link AsyncTaskLoader} so it queries the data asynchronously. Task 的自定义加载Loader,用TasksRepository当成根源。这个Loader是一个AsyncTaskLoader,所以查询数据是异步查询的。

    TasksDataSource:Main entry point for accessing tasks data.访问任务数据的主要入口点。

    For simplicity, only getTasks() and getTask() have callbacks. Consider adding callbacks to other methods to inform the user of network/database errors or successful operations.

    为简单起见,只gettasks()和gettask()有回调。考虑添加回调函数等方法来通知用户网络/数据库错误或成功的行动。

    For example, when a new task is created, it's synchronously stored in cache but usually every operation on database or network should be executed in a different thread.

    例如,当一个新的任务被创建时,它的同步存储在缓存中,但通常在数据库或网络上的每一个操作都应该在不同的线程中执行。

    TasksLoader:同TaskLoader

    TasksRepository:Concrete implementation to load tasks from the data sources into a cache.具体实现从数据源加载到缓存中的任务。

    For simplicity, this implements a dumb synchronisation between locally persisted data and data obtained from the server, by using the remote data source only if the local database doesn't exist or is empty.

    为简单起见,这里实现了一个愚蠢的同步之间的局部坚持数据和数据从服务器获取,只如果本地数据库不存在或是空的使用远程数据源。

    相关文章

      网友评论

          本文标题:Android-Architecture学习篇二:Mvp + L

          本文链接:https://www.haomeiwen.com/subject/upxupttx.html