Hello guys, if i have to do some operation after network call like saving result to db, but this operation is in fragment (scope) that will be closed, is it OK to use GlobalScope for this?
s
Sohel Shaikh
05/11/2023, 4:46 PM
Not an expert on this, but shouldn't this be handled in the data layer? Maybe repo takes care of it.
e
egor
05/11/2023, 4:52 PM
correct. but if I leave active scope it destroys my VM, stops all scopes of coroutines before I get server response and record to db won’t be done
s
Sohel Shaikh
05/11/2023, 5:02 PM
might not be best option but create a custom scope? I had similar situation and I resolved it by runBlocking and dismissing fragment only after db operation was completed
a
Anum Amin
05/11/2023, 5:19 PM
You can use create your own supervisor scope instead of viewmodel scope in order to make sure your operations are completed
e
egor
05/11/2023, 6:20 PM
thanks, but the original question about GlobalScope for there cases, is it bad for tasks like this? It runs its code and finished, what can go wrong? memory leaks?
a
Anum Amin
05/11/2023, 6:42 PM
It's a good question actually. Looking at this article, it looks like it will be scoped to your application.