How do I call a network api when the fragment dest...
# android
s
How do I call a network api when the fragment destroy? If I write api call in ViewModel with
viewModelScope
, it would be clear when view destroy. Using
Globalscope
is a good idea?
e
You could define an application wide coroutine scope in your application class, and then use that for network requests. You could also use GlobalScope here, but then you wouldn’t have any control over it (cancelling the requests would not be possible).
s
Do you have any full project example to "define an application wide coroutine scope in your application class"?
I know how to define but have no idea how to manage it
i
viewModel attached to view, so it’s gonna follow lifecycle, if you wanted to do background operation then you may be use
WorkManager
s
WorkManager
is also suitable for immediately work? In this case we need to call api to tell server side that our user leave the page (for example: purchase page)
google 1
*The user leave the page but not leave the app
i
There is OneTimeWorker and PeriodicWorker in
WorkManager
, you may be execute OneTimeWorker for immediate operation. or else in
viewmodel
you can call API and in your Repo you can change
CoroutineContext to IO
, I am not sure but you can give a try
🙌 1
e
I would personally not use WorkManager, unless you have to be absolutely sure that the operation completed. Launching a coroutine scoped to your application will be completely fine, as long as the application is alive. If you need your operation to complete even if the application is killed, use WorkManager.
🙆‍♂️ 1
I don’t have any full examples, but you don’t really need to do any extra work to maintain it, just create a scope in your application class, and inject/provide it to your classes that need it. Just make sure to create the scope with a SupervisorJob instead of a Job, so that children coroutines do not cancel the whole scope in case of an error.
🙇‍♂️ 1
t
i think you should standardise your application so that all background work is handled by work manager. Why make your application complete some work in workmanager and other work via another mechanism? this adds uneccessary complexity into your app. new developers maintaing your app will ned to learn what rules to follow when deciding how to complete background work, surely its easier to have a simple rule of ... All background work is handled via workmanager?
s
The question is: I don't think the work is "background work". Our app is always foreground in the case.
i
Did you try different CoroutineContext
(<http://Dispatchers.IO|Dispatchers.IO>)
in Repository?
s
The background is not mean background thread. this background: https://developer.android.com/about/versions/oreo/background
t
How do I call a network api when the fragment destroy?
The question is: I don't think the work is "background work".
Our app is always foreground in the case.
You are calling a network API, thats "_*background*_" work, e.g NOT executed on MAIN thread