Skip to content

Best Practice for Cancelling DataStore Flow When Component Is Destroyed Using Decompose 🛠️✨ #840

Answered by arkivanov
OsmanMusse asked this question in Q&A
Discussion options

You must be logged in to vote

As mentioned in the docs, if you are using coroutineScope(...) extension function, the scope is cancelled automatically when the component is destroyed. So there should be no need to manually cancel the jobs.

Also, most likely you want to use Dispatchers.Main.immediate instead of Dispatchers.IO. DataStore should switch the threads on its own anyway, and you should have less problems with threading when updating states etc. In particular, _state.value = _state.value.copy(currentCity = location) is not atomic, you may have problems when that code is called concurrently.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@OsmanMusse
Comment options

Answer selected by OsmanMusse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants