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

chathura :

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 items back to the listview has already been done and working fine.

My issue is that, items are stored in sqlite database and how can I delete the items from tables? (How can I know that the user has not clicked the undo button, so that I can completely remove the data from database).

This is the code inside OnOptionsItemSelcted()

case R.id.action_clear:
        final List<Word> temp = new ArrayList<Word>(data);
        data.clear();
        adapter.notifyDataSetChanged();
        View view = findViewById(R.id.layoutFavWords);
        Snackbar.make(view,"Deleted Saved Selection.", Snackbar.LENGTH_LONG).
        setAction("Undo", new OnClickListener() {

            @Override
            public void onClick(View v) {
                for(Word word:temp)
                    data.add(word);
                adapter.notifyDataSetChanged(); 
            }

        }).show();
        break;

So if the user has not clicked the undo button during the visible period of the snackbar, then I need to permanently delete the data from database.

Any solutions for this?

natario :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can i delete a file when a record is deleted from database ? (Laravel)

How can I permanently purge an entire azure keyvault that's been created with soft delete enabled?

How can I use soft deleted with relation

How to make soft delete and set column deleted=true instead of real user deleting from database?

How I can Delete Record from Firebase?

How to Delete Table from Database permanently in ROR

How to restore a soft deleted record using Laravel's Query builder?

Spring Boot/JPA - How i can delete a record from database with DeleteMapping?

how can I exclude in a thread messages from the trash before they are permanently deleted in 30 days?

Record not deleted from database

How to delete record from selected row datagridview and update database after deleted?

How can I efficiently recover a permanently deleted folder at once?

Django AutoSlugField not considering soft deleted instances by Django Safe Delete

laravel : After delete record from database the new record is in the place of the deleted record

How I Can delete all record from child table in room?

How can I delete record from table in Oracle?

Git: How to permanently delete a local commit completely from the git database

C# how can i retrieve all record from the database

How can I get the latest record from Firebase Realtime Database

Can I be sure that the name of a Linux Audit record's field is unique?

how to delete duplicates record from a mongodb database

How do I permanently delete objects from a QML canvas?

How can I delete a table from the database/ an Entity in Symfony?

How can I delete some data from Firebase Realtime Database?

How can I delete data from database after 1 hour?

How can i delete from the database multiple rows using a checkbox?

How to Implement Global Filters for Soft Delete in MongoDB Integration to Exclude Deleted Records from Queries?

How can I stop my RecyclerView, populated from Firebase Database and managed by FirebaseRecyclerAdapter, from recreating deleted views?

How can I delete record based on date?