Glide的源碼分析

glide源碼實(shí)在太冗雜了,各種設(shè)計(jì)模式各種封裝人都看到了,但是大佬寫(xiě)的確實(shí)好用,下面就幾條主線(xiàn)分析一下glide做了什么事;

1.通過(guò)glide.with 傳入context到底為了什么?

RequestManager with = Glide.with(this);
其實(shí)傳入的context可以分為兩大類(lèi)來(lái)分析:
第一類(lèi)是ApplicationContext,傳入ApplicationContext就代表當(dāng)前的這次請(qǐng)求跟程序的生命周期一致,所以就不會(huì)有感知生命周期能力了;
第二類(lèi)為activity或者fragment,這是我們重點(diǎn)要關(guān)注的,面試可能會(huì)遇到,下面針對(duì)傳入activity進(jìn)行分析;
在RequestManagerRetriever傳入activity后會(huì)調(diào)用里面的get方法,get方面里面會(huì)通過(guò)activity.getFragmentManager()獲取到FragmentManager,接著傳入到fragmentGet方面里面;

  @NonNull
  public RequestManager get(@NonNull Activity activity) {
    if (Util.isOnBackgroundThread()) {
      return get(activity.getApplicationContext());
    } else {
      assertNotDestroyed(activity);
      android.app.FragmentManager fm = activity.getFragmentManager();
      return fragmentGet(
          activity, fm, /*parentHint=*/ null, isActivityVisible(activity));
    }
  }

 @NonNull
  private RequestManager fragmentGet(@NonNull Context context,
      @NonNull android.app.FragmentManager fm,
      @Nullable android.app.Fragment parentHint,
      boolean isParentVisible) {
    RequestManagerFragment current = getRequestManagerFragment(fm, parentHint, isParentVisible);
    RequestManager requestManager = current.getRequestManager();
    if (requestManager == null) {
      // TODO(b/27524013): Factor out this Glide.get() call.
      Glide glide = Glide.get(context);
      requestManager =
          factory.build(
              glide, current.getGlideLifecycle(), current.getRequestManagerTreeNode(), context);
      current.setRequestManager(requestManager);
    }
    return requestManager;
  }

在fragmentGet方法里面創(chuàng)建自定義的RequestManagerFragment 它實(shí)際是一個(gè)fragment,這個(gè)也是感知生命周期的關(guān)鍵我們進(jìn)去看看

  @NonNull
  private RequestManagerFragment getRequestManagerFragment(
      @NonNull final android.app.FragmentManager fm,
      @Nullable android.app.Fragment parentHint,
      boolean isParentVisible) {
    RequestManagerFragment current = (RequestManagerFragment) fm.findFragmentByTag(FRAGMENT_TAG);
    if (current == null) {
      current = pendingRequestManagerFragments.get(fm);
      if (current == null) {
        current = new RequestManagerFragment();
        current.setParentFragmentHint(parentHint);
        if (isParentVisible) {
          current.getGlideLifecycle().onStart();
        }
        pendingRequestManagerFragments.put(fm, current);
        fm.beginTransaction().add(current, FRAGMENT_TAG).commitAllowingStateLoss();
        handler.obtainMessage(ID_REMOVE_FRAGMENT_MANAGER, fm).sendToTarget();
      }
    }
    return current;
  }

這個(gè)主要是把RequestManagerFragment通過(guò)事務(wù)管理添加到activity的FragmentManager里面進(jìn)行綁定,這樣activity的生命周期就可以被RequestManagerFragment感知到了,接著由于RequestManager實(shí)現(xiàn)LifecycleListener接口里面有onStart,onStop,onDestroy方法,我們把創(chuàng)建的RequestManager傳入到RequestManagerFragment里面進(jìn)行綁定,這樣就串起來(lái),activity調(diào)用onstart----》RequestManagerFragment.onstart---》FragmentManager.onstart---->RequestTracker開(kāi)始從集合里面拿到request調(diào)用begin開(kāi)始請(qǐng)求圖片
activity調(diào)用onstop---》RequestManagerFragment.onstop--》FragmentManager.onstop---->RequestTracker里面將處于請(qǐng)求集合的request清除掉并添加到等待處理的集合里面

2 Glide隊(duì)列請(qǐng)求如何處理的?

當(dāng)我們load.into(iv);方法時(shí),所有的請(qǐng)求都會(huì)被添加到一個(gè)叫RequestTracker的集合中,而且這個(gè)集合有兩個(gè)
一個(gè)是運(yùn)行時(shí)集合,一個(gè)是等待集合;
如果當(dāng)前頁(yè)面停止,onStop方法被調(diào)用,所有的運(yùn)行中的請(qǐng)求都會(huì)停止,并且全部添加到等待集合中;
當(dāng)開(kāi)始運(yùn)行時(shí),又會(huì)把所有等待集合中的請(qǐng)求 放到運(yùn)行中去!

3 怎么處理請(qǐng)求

1:當(dāng)RequestManager調(diào)用requestTracker.runRequest的時(shí)候,
2: 所有的請(qǐng)求都會(huì)構(gòu)建成一個(gè)SingleRequest,并且開(kāi)始調(diào)用begin方法運(yùn)行;
3:begin方法最終會(huì)調(diào)用engine.load方法
4:根據(jù)request構(gòu)建EngineKey
5:根據(jù)EngineKey去活動(dòng)緩存中獲取數(shù)據(jù)
6:如果獲取不到,去內(nèi)存緩存中獲取數(shù)據(jù)
7:如果獲取不到,通過(guò)硬盤(pán)緩存的線(xiàn)程池去獲取本地硬盤(pán)的數(shù)據(jù)
8:如果獲取不到本地的,通過(guò)網(wǎng)絡(luò)的線(xiàn)程池去獲取網(wǎng)絡(luò)的數(shù)據(jù)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容