在Android Loader源碼分析(一)中說(shuō)道在start方法的最后,會(huì)調(diào)用mLoader.startLoading();
startLoading方法又會(huì)調(diào)用onStartLoading方法
//在Loader.java文件中
public final void startLoading() {
mStarted = true;
mReset = false;
mAbandoned = false;
onStartLoading();
}
//在CursorLoader.java 文件中
@Override
protected void onStartLoading() {
if (mCursor != null) {
deliverResult(mCursor);
}
if (takeContentChanged() || mCursor == null) {
forceLoad();
}
}
public void forceLoad() {
onForceLoad();
}
在數(shù)據(jù)還未加載時(shí)肚医,mCursor為null匙奴,此時(shí)會(huì)執(zhí)行forceLoad()
forceLoad()會(huì)執(zhí)行onForceLoad()叙赚,此方法的實(shí)現(xiàn)是在AsyncTaskLoader.java中
@Override
protected void onForceLoad() {
super.onForceLoad();
cancelLoad();
mTask = new LoadTask();
if (DEBUG) Log.v(TAG, "Preparing load: mTask=" + mTask);
executePendingTask();
}
(1)怨咪、forceLoad()會(huì)調(diào)用onForceLoad()。而onForceLoad()中會(huì)新建LoadTask對(duì)象孟抗,然后執(zhí)行executePendingTask()迁杨。
(2)、在executePendingTask()中會(huì)調(diào)用LoadTask對(duì)象的executeOnExecutor()凄硼。
void executePendingTask() {
if (mCancellingTask == null && mTask != null) {
if (mTask.waiting) {
mTask.waiting = false;
mHandler.removeCallbacks(mTask);
}
if (mUpdateThrottle > 0) {
long now = SystemClock.uptimeMillis();
if (now < (mLastLoadCompleteTime+mUpdateThrottle)) {
// Not yet time to do another load.
mTask.waiting = true;
mHandler.postAtTime(mTask, mLastLoadCompleteTime+mUpdateThrottle);
return;
}
}
mTask.executeOnExecutor(mExecutor, (Void[]) null);
}
}
//LoadTask的實(shí)現(xiàn)
final class LoadTask extends AsyncTask<Void, Void, D> implements Runnable {
private final CountDownLatch mDone = new CountDownLatch(1);
boolean waiting;
/* Runs on a worker thread */
@Override
protected D doInBackground(Void... params) {
try {
D data = AsyncTaskLoader.this.onLoadInBackground();
return data;
} catch (OperationCanceledException ex) {
if (!isCancelled()) {
throw ex;
}
return null;
}
}
/* Runs on the UI thread */
@Override
protected void onPostExecute(D data) {
try {
AsyncTaskLoader.this.dispatchOnLoadComplete(this, data);
} finally {
mDone.countDown();
}
}
/* Runs on the UI thread */
@Override
protected void onCancelled(D data) {
try {
AsyncTaskLoader.this.dispatchOnCancelled(this, data);
} finally {
mDone.countDown();
}
}
/* Runs on the UI thread, when the waiting task is posted to a handler.
* This method is only executed when task execution was deferred (waiting was true). */
@Override
public void run() {
waiting = false;
AsyncTaskLoader.this.executePendingTask();
}
/* Used for testing purposes to wait for the task to complete. */
public void waitForLoader() {
try {
mDone.await();
} catch (InterruptedException e) {
// Ignore
}
}
}
- LoadTask是AsyncTaskLoader的內(nèi)部類铅协。實(shí)際上,它是AsyncTask的子類摊沉,executeOnExecutor()會(huì)將任務(wù)提交到線程池中去執(zhí)行狐史;
- 這個(gè)被提交到線程池的任務(wù)會(huì)執(zhí)行AsyncTask的doInBackground()。
- LoadTask的doInBackground()會(huì)調(diào)用onLoadInBackground()
- onLoadInBackground()方法會(huì)調(diào)用loadInBackground() 這個(gè)方法是在CursorLoader中實(shí)現(xiàn)的
/* Runs on a worker thread */
@Override
public Cursor loadInBackground() {
synchronized (this) {
if (isLoadInBackgroundCanceled()) {
throw new OperationCanceledException();
}
mCancellationSignal = new CancellationSignal();
}
try {
Cursor cursor = getContext().getContentResolver().query(mUri, mProjection, mSelection,
mSelectionArgs, mSortOrder, mCancellationSignal);
if (cursor != null) {
try {
// Ensure the cursor window is filled.
cursor.getCount();
cursor.registerContentObserver(mObserver);
} catch (RuntimeException ex) {
cursor.close();
throw ex;
}
}
return cursor;
} finally {
synchronized (this) {
mCancellationSignal = null;
}
}
}
- 當(dāng)AsyncTask中的任務(wù)執(zhí)行完時(shí)说墨,會(huì)通過(guò)onPostExecute()反饋執(zhí)行結(jié)果骏全。
- onPostExecute()會(huì)執(zhí)行dispatchOnLoadComplete(),而后者會(huì)調(diào)用deliverResult()來(lái)分發(fā)消息尼斧。
- 最終會(huì)執(zhí)行mListener.onLoadComplete()姜贡。mListener是什么呢?它是我們?cè)趫?zhí)行LoaderManager.java的start()函數(shù)時(shí)突颊,通過(guò)mLoader.registerListener(mId, this)注冊(cè)到Loader上的鲁豪。也就是說(shuō)潘悼,mListener是LoaderManager中的LoaderInfo對(duì)象律秃。