In a snackbar action, how can I be sure it's safe to permanently delete a soft-deleted record from the database?

后端 未结 5 1259
轻奢々
轻奢々 2021-02-04 02:29

I am using Snackbar in android and I have implemented an action so that user can undo the action (the action is clearing all the items in the listview).Removing and adding the i

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 03:10

    As far as I know, it is by design. You should:

    • Delete the item as soon as the user taps the delete button;
    • Store it temporarily in a class variable;
    • If the user taps Undo, add the item again to the database.

    This approach is safer and more robust; you shouldn't wait for the snackbar to be dismissed, because that action could not even happen. Just think of user force-quitting the app while the snackbar is still on: should the item be deleted or not? It should.

    A more trustworthy source is g+ post by Ian Lake (deleted because of G+ deprecation). In the comments you can read:

    you want your UI to react immediately (not wait for the snackbar to disappear) - most systems (particularly those that sync to an external server) have the concept of a 'soft delete' where things are marked as deleted. In those cases, an undo action would just be unmarking the record as deleted. This system works even if the user were to leave the app before the snackbar finishes (you can't assume the snackbar will always complete its animation!).

    The easiest way to do that is to temporarily save the record elsewhere (even a local variable), then re-insert it if they happen to hit the undo button.

提交回复
热议问题