Last active
May 14, 2020 03:10
-
-
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?
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
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