Created
September 13, 2019 21:36
-
-
Save De-Morgan/e1b95d5ee2ce67a0ea7294718f9a9a98 to your computer and use it in GitHub Desktop.
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 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