https://kotlinlang.org logo
#coroutines
Title
# coroutines
s

scottiedog45

09/14/2019, 6:21 PM
If I’m using this
liveData
block, do I need to scope it to
viewModelScope
, or is that handled automatically? Specifically I’m concerned about cancelling the job, does that still need to be done with this method?
Copy code
val testLangLiveData : LiveData<String?> = liveData {
        viewModelScope.launch {
            try {
                emit(repository.getUser().data.attributes?.locale)
            } catch (e: Exception) {

            }
        }
    }
a

Adam Powell

09/14/2019, 6:26 PM
You don't need the inner launch, once there are no more observers the job executing the block is cancelled for you
🙌 1
s

scottiedog45

09/14/2019, 6:31 PM
Got it- thanks Adam!
👍 1
3 Views