oysteinkrog / SQLite.Net-PCL

Simple, powerful, cross-platform SQLite client and ORM - Updated version with PCL support
MIT License
353 stars 162 forks source link

How does RollbackTo work? #336

Closed ghost closed 7 years ago

ghost commented 8 years ago

Currently I'm a bit struggling with the RollbackTo function of SQLite. I want to undo some changes that are made (e.g. deleting an item, changing a property of the item), so I make use of transactions. I've made an RunInTransactionWithUndoAsync extension for the SQLiteConnection. It performs the following steps:

  1. It saves a transaction point, saved in a var
  2. It invokes the action, e.g. updating a bunch of items
  3. It tells the UI (using MVVM messaging) to show a dialog for five seconds that gives the user the chance to undo his changes
  4. Meanwhile a Task.Delay(5000) is executed.
  5. If the user doesn't do anything, the changes will be committed via Commit(). Otherwise they will be undone through RollbackTo(transactionPoint)

So far, so good. Anything related to the database works fine, but there's one problem. When I'm undoing the changes the UI changes will be reverted as well, which would be fine, if it would work correctly. So when I'm marking item 5 from 100 items in my ObservableCollection<ItemViewModel> on my MainViewModel as read (in my example; it's just a boolean) and rollback those changes, SQLite adds the last item (item 100) another time to the collection, which is not the wanted behavior. If you could explain to me why this is happening, that would be nice. It's not my code who's doing the UI changes after rollback, because even after commenting out all the places, where I modify that collection, the CollectionChanged event fires after a rollback.

ghost commented 7 years ago

It seems that my problem was related to events, not to the actual SQLite behavior.