From 8cf8597c449901debdd9baae5d3d390814d30b81 Mon Sep 17 00:00:00 2001 From: Darshan Parajuli Date: Mon, 17 Feb 2020 20:08:29 -0800 Subject: [PATCH] Use viewModelScope --- .../com/dp/logcatapp/util/ScopedViewModel.kt | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/dp/logcatapp/util/ScopedViewModel.kt b/app/src/main/java/com/dp/logcatapp/util/ScopedViewModel.kt index 8bda8125..b2ed8e28 100644 --- a/app/src/main/java/com/dp/logcatapp/util/ScopedViewModel.kt +++ b/app/src/main/java/com/dp/logcatapp/util/ScopedViewModel.kt @@ -3,29 +3,17 @@ package com.dp.logcatapp.util import android.app.Application import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.ViewModel +import androidx.lifecycle.viewModelScope import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers.Main -import kotlinx.coroutines.Job import kotlin.coroutines.CoroutineContext open class ScopedViewModel : ViewModel(), CoroutineScope { - private val job = Job() - override val coroutineContext: CoroutineContext - get() = job + Main - - override fun onCleared() { - job.cancel() - } + get() = viewModelScope.coroutineContext } open class ScopedAndroidViewModel(application: Application) : AndroidViewModel(application), CoroutineScope { - private val job = Job() override val coroutineContext: CoroutineContext - get() = job + Main - - override fun onCleared() { - job.cancel() - } + get() = viewModelScope.coroutineContext }