Last active
July 29, 2020 14:14
-
-
Save tizisdeepan/fc24bf774ada080eb17d2ad4a4ab0c1a to your computer and use it in GitHub Desktop.
MVVM with LiveData
This file contains 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
onCreate() { | |
val viewModel = ... //initialize your view model | |
viewModel.getData() | |
viewModel.data.observe(this, Observer { | |
//do operations based on the value | |
}) | |
} |
This file contains 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
private var parentJob = Job() | |
private val coroutineContext: CoroutineContext | |
get() = parentJob + Dispatchers.IO | |
private val scope = CoroutineScope(coroutineContext) | |
val data: LiveData<Data> = MutableLiveData() | |
fun getData() { | |
scope.launch { | |
//do network calls | |
override onSuccess(data: Data) { | |
data.value = data | |
} | |
} | |
} |
This file contains 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
val data: LiveData<Data> = MutableLiveData() | |
fun getData() { | |
data = repository.getData() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment