Philippe Boisney
04/07/2019, 3:14 PMNetworkBoundResource
with Coroutines (instead of Executor
like the Google sample https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/repository/NetworkBoundResource.kt). I also want that NetworkBoundResource
return a LiveData
through the method asLiveData()
.
1️⃣ My first try was to pass a CoroutineScope
to the constructor of NetworkBoundResource
, to properly manage cancellation.
Pros: in this way, tasks are canceled when a ViewModel is cleared for example.
Cons: My UserRepository
methods ask for parameter a CoroutineScope
for each call
🔗 https://gist.github.com/PhilippeBoisney/47b9eaee02df537cf7b0703707243a78
2️⃣ My second try was to use a GlobalScope
to launch the Coroutines inside the NetworkBoundResource
.
Pros: No more CoroutineScope
passed through `UserRepository`methods parameters.
Cons: GlobalScope
should be avoided (even if in Google sample, they use Executor
the same way of a GlobalScope
)
🔗 https://gist.github.com/PhilippeBoisney/4a5fc718a2b8d9735bdf2a5bfb37aebe
Which one of those approaches do you prefer? If none, how will you do? :thread-please:streetsofboston
04/07/2019, 5:50 PMCoroutineScope
.
4️⃣ Declare the methods of the UserRepository to be suspend
dewildte
04/08/2019, 4:11 PMjishindev
04/22/2019, 8:24 AMval result = loadData<ResponseBody, LogoutError> {
apiInterface.logout()
}
I can pass in a db call lambda if i need to get cached data from the db.