Skip to content

Instantly share code, notes, and snippets.

@De-Morgan
Created September 13, 2019 21:36
Show Gist options
  • Save De-Morgan/e1b95d5ee2ce67a0ea7294718f9a9a98 to your computer and use it in GitHub Desktop.
Save De-Morgan/e1b95d5ee2ce67a0ea7294718f9a9a98 to your computer and use it in GitHub Desktop.
class PostsOverviewViewModel : ViewModel() {
private val _posts = MutableLiveData<List<Post>>()
val post: LiveData<List<Post>>
get() = _posts
init {
getPosts()
}
private fun getPosts() {
viewModelScope.launch {
val getDeferredPosts = PostApi.retrofitService.getPosts()
try {
val listResult = getDeferredPosts
if(listResult.isNotEmpty()){
_posts.value = listResult
}
}catch (e:Exception){
_posts.value = ArrayList()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment