最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文了解Android?ViewModelScope?如何自動取消協(xié)程

 更新時間:2022年07月03日 10:43:41   作者:??阿鍋阿鍋??????  
這篇文章主要介紹了一文了解Android?ViewModelScope?如何自動取消協(xié)程,文章圍繞主題站展開詳細(xì)的內(nèi)容介紹,具有一定參考價值,感興趣的小伙伴可以參考一下

先看一下 ViewModel 中的 ViewModelScope 是何方神圣

val ViewModel.viewModelScope: CoroutineScope
        get() {
            val scope: CoroutineScope? = this.getTag(JOB_KEY)
            if (scope != null) {
                return scope
            }
            return setTagIfAbsent(JOB_KEY,
                CloseableCoroutineScope(SupervisorJob() + Dispatchers.Main.immediate))
        }

可以看到這個是一個擴(kuò)展方法,

再點(diǎn)擊 setTagIfAbsent 方法進(jìn)去

 <T> T setTagIfAbsent(String key, T newValue) {
        T previous;
        synchronized (mBagOfTags) {
            previous = (T) mBagOfTags.get(key);//第一次肯定為null
            if (previous == null) {
                mBagOfTags.put(key, newValue);//null 存儲
            }
        }
        T result = previous == null ? newValue : previous;
        if (mCleared) {//判斷是否已經(jīng)clear了
            // It is possible that we'll call close() multiple times on the same object, but
            // Closeable interface requires close method to be idempotent:
            // "if the stream is already closed then invoking this method has no effect." (c)
            closeWithRuntimeException(result);
        }
        return result;
    }

可以看到 這邊 會把 我們的 ViewModel 存儲到 ViewModel 內(nèi)的 mBagOfTags 中

這個 mBagOfTags 是

    private final Map<String, Object> mBagOfTags = new HashMap<>();

這個時候 我們 viewModel 就會持有 我們 viewModelScope 的協(xié)程 作用域了。那..這也只是 表述了 我們 viewModelScope 存在哪里而已,什么時候清除呢?

先看一下 ViewModel 的生命周期:

可以看到 ViewModel 的生命周期 會在 Activity onDestory 之后會被調(diào)用。那...具體哪里調(diào)的?

翻看源碼可以追溯到 ComponentActivity 的默認(rèn)構(gòu)造器內(nèi)

 public ComponentActivity() {
     /*省略一些*/
        getLifecycle().addObserver(new LifecycleEventObserver() {
            @Override
            public void onStateChanged(@NonNull LifecycleOwner source,
                    @NonNull Lifecycle.Event event) {
                if (event == Lifecycle.Event.ON_DESTROY) {
                    if (!isChangingConfigurations()) {
                        getViewModelStore().clear();
                    }
                }
            }
        });
  }

可以看到內(nèi)部會通對 Lifecycle 添加一個觀察者,觀察當(dāng)前 Activity 的生命周期變更事件,如果走到了 Destory ,并且 本次 Destory 并非由于配置變更引起的,才會真正調(diào)用 ViewModelStore 的 clear 方法。

跟進(jìn) clear 方法看看:

public class ViewModelStore {
    private final HashMap<String, ViewModel> mMap = new HashMap<>();

    /**
     *  Clears internal storage and notifies ViewModels that they are no longer used.
     */
    public final void clear() {
        for (ViewModel vm : mMap.values()) {
            vm.clear();
        }
        mMap.clear();
    }
}

可以看到這個 ViewModelStore 內(nèi)部實(shí)現(xiàn) 用 HashMap 存儲 ViewModel

于是在 clear 的時候,會逐個遍歷調(diào)用 clear方法,再次跟進(jìn) ViewModel 的 clear 方法

 @MainThread
    final void clear() {
        mCleared = true;
        // Since clear() is final, this method is still called on mock objects
        // and in those cases, mBagOfTags is null. It'll always be empty though
        // because setTagIfAbsent and getTag are not final so we can skip
        // clearing it
        if (mBagOfTags != null) {
            synchronized (mBagOfTags) {
                for (Object value : mBagOfTags.values()) {
                    // see comment for the similar call in setTagIfAbsent
                    closeWithRuntimeException(value);
                }
            }
        }
        onCleared();
    }

可以發(fā)現(xiàn)我們最初 存放 viewmodelScope 的 mBagOfTags

這里面的邏輯 就是對 mBagOfTags 存儲的數(shù)據(jù) 挨個提取出來并且調(diào)用 closeWithRuntimeException

跟進(jìn) closeWithRuntimeException:

 private static void closeWithRuntimeException(Object obj) {
        if (obj instanceof Closeable) {
            try {
                ((Closeable) obj).close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

該方法內(nèi)會逐個判斷 對象是否實(shí)現(xiàn) Closeable 如果實(shí)現(xiàn)就會調(diào)用這個接口的 close 方法,

再回到最初 我們 viewModel 的擴(kuò)展方法那邊,看看我們 viewModelScope 的真正面目

internal class CloseableCoroutineScope(context: CoroutineContext) 
    : Closeable, CoroutineScope {
    override val coroutineContext: CoroutineContext = context

    override fun close() {
        coroutineContext.cancel()
    }
}

可以明確的看到 我們的 ViewModelScope 實(shí)現(xiàn)了 Closeable 并且充寫了 close 方法,

close 方法內(nèi)的實(shí)現(xiàn) 會對 協(xié)程上下文進(jìn)行 cancel。

至此我們 可以大致整理一下:

  • viewModelScope 是 ViewModel 的擴(kuò)展成員,該對象是 CloseableCoroutineScope,并且實(shí)現(xiàn)了 Closeable 接口
  • ViewModelScope 存儲在 ViewModel 的 名叫 mBagOfTags 的HashMap中 啊
  • ViewModel 存儲在 Activity 的 ViewModelStore 中,并且會監(jiān)聽 Activity 的 Lifecycle 的狀態(tài)變更,在ON_DESTROY 且 非配置變更引起的事件中 對 viewModelStore 進(jìn)行清空
  • ViewModelStore 清空會對 ViewModelStore 內(nèi)的所有 ViewModel 逐個調(diào)用 clear 方法。
  • ViewModel的clear方法會對 ViewModel的 mBagOfTags 內(nèi)存儲的對象進(jìn)行調(diào)用 close 方法(該對象需實(shí)現(xiàn)Closeable 接口)
  • 最終會會調(diào)用 我們 ViewModelScope 的實(shí)現(xiàn)類 CloseableCoroutineScope 的 close 方法中。close 方法會對協(xié)程進(jìn)行 cancel。

到此這篇關(guān)于一文了解Android ViewModelScope 如何自動取消協(xié)程的文章就介紹到這了,更多相關(guān)Android ViewModel Scope 取消協(xié)程內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android中不支持動態(tài)申請權(quán)限的原因

    Android中不支持動態(tài)申請權(quán)限的原因

    這篇文章主要介紹了Android中不支持動態(tài)申請權(quán)限的原因,本文列舉了幾個不支持動態(tài)申請權(quán)限的原因,需要的朋友可以參考下
    2015-01-01
  • android使用DataBinding來設(shè)置空狀態(tài)

    android使用DataBinding來設(shè)置空狀態(tài)

    本篇文章主要介紹了android使用DataBinding來設(shè)置空狀態(tài),具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • Android 中WebView 截圖的實(shí)現(xiàn)方式

    Android 中WebView 截圖的實(shí)現(xiàn)方式

    這篇文章主要介紹了Android 中WebView 截圖的實(shí)現(xiàn)方式,WebView 作為一種特殊的控件,自然不能像其他系統(tǒng) View 或者截屏的方式來獲取截圖。本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧
    2017-12-12
  • Android Service判斷設(shè)備聯(lián)網(wǎng)狀態(tài)詳解

    Android Service判斷設(shè)備聯(lián)網(wǎng)狀態(tài)詳解

    本文主要介紹Android Service判斷聯(lián)網(wǎng)狀態(tài),這里提供了相關(guān)資料并附有示例代碼,有興趣的小伙伴可以參考下,幫助開發(fā)相關(guān)應(yīng)用功能
    2016-08-08
  • Android開發(fā)案例手冊Application跳出dialog

    Android開發(fā)案例手冊Application跳出dialog

    這篇文章主要為大家介紹了Android開發(fā)案例手冊Application跳出dialog,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • Android通過單點(diǎn)觸摸移動圖片

    Android通過單點(diǎn)觸摸移動圖片

    這篇文章主要為大家詳細(xì)介紹了Android通過單點(diǎn)觸摸移動圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android實(shí)現(xiàn)調(diào)用攝像頭

    Android實(shí)現(xiàn)調(diào)用攝像頭

    本文給大家分享的是,在安卓APP開發(fā)的過程中,經(jīng)常會需要調(diào)用手機(jī)自身攝像頭拍照的代碼,十分的簡單實(shí)用,有需要的小伙伴可以參考下。
    2015-07-07
  • android不同activity之間共享數(shù)據(jù)解決方法

    android不同activity之間共享數(shù)據(jù)解決方法

    最近做局域網(wǎng)socket連接問題,要在多個activity之間公用一個socket連接,就在網(wǎng)上搜了下資料,感覺還是application方法好用,帖出來需要的朋友可以參考下
    2012-11-11
  • Android加載Gif動畫實(shí)現(xiàn)代碼

    Android加載Gif動畫實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Android加載Gif動畫實(shí)現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • android Watchdog 實(shí)現(xiàn)剖析

    android Watchdog 實(shí)現(xiàn)剖析

    Android提供了Watchdog類,用來監(jiān)測Service是否處于正常工作中,是在SystemServer中啟動的;本文將詳細(xì)介紹
    2012-11-11

最新評論

永吉县| 本溪市| 锡林浩特市| 清镇市| 西乌珠穆沁旗| 泸水县| 阿克| 疏勒县| 碌曲县| 松溪县| 黄石市| 方城县| 麻城市| 迭部县| 富裕县| 武义县| 高淳县| 吉木萨尔县| 昌邑市| 榆社县| 蓬安县| 尼木县| 西丰县| 外汇| 绥中县| 宣威市| 海安县| 四川省| 湖南省| 昭平县| 清远市| 无锡市| 志丹县| 双柏县| 云南省| 共和县| 措美县| 临漳县| 克山县| 定襄县| 郑州市|