Reputation: 73
https://developer.android.com/topic/architecture/data-layer#in-memory-cache
While browsing through these documentation, I saw that Coroutines
have super easy control over the fact that Internet connections are made from the AppCoroutineContext
and not from the viewModel
.
If the user navigates away from the screen while the network request is in progress, it'll be canceled and the result won't be cached. NewsRepository shouldn't use the caller's CoroutineScope to perform this logic. Instead, NewsRepository should use a CoroutineScope that's attached to its lifecycle. Fetching the latest news needs to be an app-oriented operation.
Do you know how to apply it with RxJava? I would have to subscribe to the Application context
. When then do you call dispose ()
?
It is possible?
Upvotes: 0
Views: 26
Reputation: 1121
If you really don't want a network call to be cancelled, you should put it in a Service.
If you want your call to be tied to the application lifecycle you don't really need to worry about it. Just do the subscription in an application scoped singleton object - this is pretty easy if you Hilt and annotate your object as @Singleton
then inject it where you need it.
Generally, you shouldn't need to do that so I'd recommend either looking into a service or putting your network call in the view model.
Upvotes: 0