Skip to content

Instantly share code, notes, and snippets.

@sberoch
Created July 13, 2020 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sberoch/457a4550fa1bf1f2e8c0a057e48c87b5 to your computer and use it in GitHub Desktop.
Save sberoch/457a4550fa1bf1f2e8c0a057e48c87b5 to your computer and use it in GitHub Desktop.
fun <T, A> performGetOperation(databaseQuery: () -> LiveData<T>,
networkCall: suspend () -> Resource<A>,
saveCallResult: suspend (A) -> Unit): LiveData<Resource<T>> =
liveData(Dispatchers.IO) {
emit(Resource.loading())
val source = databaseQuery.invoke().map { Resource.success(it) }
emitSource(source)
val responseStatus = networkCall.invoke()
if (responseStatus.status == SUCCESS) {
saveCallResult(responseStatus.data!!)
} else if (responseStatus.status == ERROR) {
emit(Resource.error(responseStatus.message!!))
emitSource(source)
}
}
@PatricioIN
Copy link

I'm trying to move this file to the data layer but it's impossible, because it's pure java, and LiveData can't be imported. Any solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment