Skip to content

Instantly share code, notes, and snippets.

@prfarlow1
Last active May 14, 2020 03:10
Show Gist options
  • Save prfarlow1/c48f556c8b5b177ab8e787729973341e to your computer and use it in GitHub Desktop.
Save prfarlow1/c48f556c8b5b177ab8e787729973341e to your computer and use it in GitHub Desktop.
I'm having trouble understanding best practices around the liveData builder function. Is one of these example incorrect? If not, what guidelines would help me pick whether to build the LiveData in my ViewModel layer vs my repository layer?
class MyViewModelOne(private val dataRepositoryOne: DataRepositoryOne) : ViewModel() {
fun fetchDataButtonClicked() = liveData {
emit(dataRepositoryOne.fetchData())
}
}
class DataRepositoryOne(private val webInterface: MyWebInterface) {
suspend fun fetchData() = webInterface.fetchWebData()
}
class MyViewModelTwo(private val dataRepositoryTwo: DataRepositoryTwo) : ViewModel() {
fun fetchDataButtonClicked() = dataRepositoryTwo.fetchData()
}
class DataRepositoryTwo(private val webInterface: MyWebInterface) {
fun fetchData() = liveData {
emit(webInterface.fetchWebData())
}
}
interface MyWebInterface {
// this is probably a Retrofit interface
suspend fun fetchWebData(): List<String>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment