This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyViewModel : ViewModel(), CoroutineScope { | |
| private val job = SupervisorJob() | |
| override val coroutineContext: CoroutineContext | |
| get() = SupervisorJob() + Dispatchers.Default | |
| override fun onCleared() { | |
| job.cancel() | |
| super.onCleared() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyViewModel : BaseViewModel() // nothing at all to implement! | |
| abstract class BaseViewModel : ViewModel(), CoroutineScope { | |
| private val job = SupervisorJob() | |
| override val coroutineContext: CoroutineContext | |
| get() = job + Dispatchers.Default | |
| override fun onCleared() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun SupervisorCoroutineScope( | |
| dispatcher: CoroutineDispatcher | |
| ): CoroutineScope = object : CoroutineScope { | |
| override val coroutineContext: CoroutineContext | |
| get() = SupervisorJob() + dispatcher | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| abstract class BaseViewModel : | |
| ViewModel(), | |
| CoroutineScope by SupervisorCoroutineScope(Dispatchers.Default) { | |
| override fun onCleared() { | |
| cancel() | |
| super.onCleared() | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyRepositoryImpl(coroutineScope: CoroutineScope) : | |
| MyRepository, CoroutineScope by coroutineScope |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyViewModel : BaseViewModel() { | |
| val myRepository = MyRepositoryImpl( | |
| coroutineScope = this + Dispatchers.IO | |
| ) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyViewModel( | |
| private val myRepository: MyRepository, | |
| coroutineScope: CoroutineScope | |
| ) : BaseViewModel(coroutineScope) | |
| abstract class BaseViewModel( | |
| coroutineScope: CoroutineScope | |
| ) : ViewModel(), CoroutineScope by coroutineScope { | |
| override fun onCleared() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| viewModelFactory { | |
| val scope = SupervisorCoroutineScope(Dispatchers.Default) | |
| MyViewModel(MyRepository(scope), scope) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlinx.coroutines.Dispatchers | |
| import kotlinx.coroutines.test.TestCoroutineDispatcher | |
| import kotlinx.coroutines.test.TestCoroutineScope | |
| import kotlinx.coroutines.test.resetMain | |
| import kotlinx.coroutines.test.setMain | |
| import org.junit.jupiter.api.extension.AfterAllCallback | |
| import org.junit.jupiter.api.extension.AfterEachCallback | |
| import org.junit.jupiter.api.extension.BeforeAllCallback | |
| import org.junit.jupiter.api.extension.ExtendWith | |
| import org.junit.jupiter.api.extension.ExtensionContext |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyViewModel : ViewModel() { | |
| val scope : CoroutineScope = MainScope() | |
| override fun onCleared() { | |
| scope.cancel() | |
| super.onCleared() | |
| } | |
| } |
OlderNewer