I’m working on a project saving data locally with ...
# android
j
I’m working on a project saving data locally with Room. I know Room offload the main thread and we don’t need to write
withContext(<http://Dispatchers.IO|Dispatchers.IO>)
but my concern is on the coroutine scope. Does Room guarantee inserts or is it possible that it doesn’t go through if the insert is done from a
viewModelScope
for example and the view is destroyed at the same time? Should I even consider using
WorkManager
to be sure my inserts work even if the app is killed?
b
Does Room guarantee inserts or is it possible that it doesn’t go through if the insert is done from a
viewModelScope
for example and the view is destroyed at the same time?
No, it doesn't guarantee insert if vmScope gets cancelled. You can wrap your insert into
withContext(NonCancellable)
for this particular case if you want it to be inserted regardless of VM's scope cancellation. Another option is to launch it in a wider scope (e.g. app scope). Both options won't help in case of process' death (I don't think you have anything to do with it)
Should I even consider using
WorkManager
to be sure my inserts work even if the app is killed? (edited)
I don't think so because of how WM works under the hood. Internally it uses Room DB as well, that means your work is asynchronously persisted to the library table. That means it also won't help if process is dying during insert transaction.
j
thanks for the tips 👍