Skip to content

How to prevent race condition when save data on Drift #3254

Answered by simolus3
rizaldi-wiratama asked this question in Q&A
Discussion options

You must be logged in to vote

Yes! You can run multiple statements atomically (with an implicit lock on the database) by using transactions:

saveOrder(Order updatedOrder) async {
  await database.transaction(() async {
    Order? localOrder = await database.managers.orders....;
    // Rest of your update logic here
  })
}

Transactions essentially behave as if all their statements inside ran as one (which also means that no concurrent invocation of saveOrder can cause race conditions).

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by rizaldi-wiratama
Comment options

You must be logged in to vote
0 replies
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