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用任務請求獲取數(shù)據(jù)
2 基本結構
The advantages of Loaders, from theLoaders documentation page, are:
從Loaders文檔上可以看出担败,Loaders的優(yōu)點是:
They provide asynchronous loading of data, removing the need for callbacks in the repository.
Loaders提供異步加載數(shù)據(jù),移除庫repository中需要的請求回調。
They monitor the source of their data and deliver new results when the content changes, in our case, the repository.
Loaders會監(jiān)視數(shù)據(jù)來源,根據(jù)數(shù)據(jù)內容的變化,提供最新的的結果。
They automatically reconnect to the last loader when being recreated after a configuration change.
當Loaders被重新改變配置后,會自動重新連接到最后一個加載程序慎框。
3 Loaders實現(xiàn)目錄結構
4 Loaders實現(xiàn)分析
The Loaders (TaskLoaderandTasksLoader) are responsible for fetching the data and extend AsyncTaskLoader.
TaskLoader和TasksLoader都是繼承AsyncTaskLoader,負責取數(shù)據(jù)舆绎。
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處理。
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也使用加載數(shù)據(jù)吕朵,就像在MVP例子里一樣猎醇,但是它要通過LoaderManager來實現(xiàn)。
@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在這種情況下梧税,是一個簡單的強制加載數(shù)據(jù)的Loader
In?TasksLoader.java:
@Override
public void onTasksChanged() {
? ?if(isStarted()) {?
?? ? ? forceLoad();??
? ? }
}
6 類的解釋
Task:Immutable model class for a Task. 不可改變的Task模型
TasksDbHelper:Task數(shù)據(jù)幫助類
TasksLocalDataSource:Concrete implementation of a data source as a db. 作為數(shù)據(jù)庫的數(shù)據(jù)源的具體實現(xiàn)沦疾。
TasksPersistenceContract:The contract used for the db to save the tasks locally. 用于保存tasks保存到本地數(shù)據(jù)庫的Contract
TasksRemoteDataSource:Implementation of the data source that adds a latency simulating network.添加一個延遲模擬網絡的數(shù)據(jù)源的實現(xiàn)。
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,所以查詢數(shù)據(jù)是異步查詢的凳谦。
TasksDataSource:Main entry point for accessing tasks data.訪問任務數(shù)據(jù)的主要入口點忆畅。
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()有回調尸执〖铱考慮添加回調函數(shù)等方法來通知用戶網絡/數(shù)據(jù)庫錯誤或成功的行動。
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.
例如如失,當一個新的任務被創(chuàng)建時绊诲,它的同步存儲在緩存中,但通常在數(shù)據(jù)庫或網絡上的每一個操作都應該在不同的線程中執(zhí)行褪贵。
TasksLoader:同TaskLoader
TasksRepository:Concrete implementation to load tasks from the data sources into a cache.具體實現(xiàn)從數(shù)據(jù)源加載到緩存中的任務掂之。
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.
為簡單起見,這里實現(xiàn)了一個愚蠢的同步之間的局部堅持數(shù)據(jù)和數(shù)據(jù)從服務器獲取,只如果本地數(shù)據(jù)庫不存在或是空的使用遠程數(shù)據(jù)源板惑。