在小吃店操作中,如何确定从数据库中永久删除软删除的记录是安全的? [英] In a snackbar action, how can I be sure it's safe to permanently delete a soft-deleted record from the database?

查看:137
本文介绍了在小吃店操作中,如何确定从数据库中永久删除软删除的记录是安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在android中使用Snackbar,并且已经实现了一个操作,以便用户可以撤消该操作(该操作正在清除列表视图中的所有项目).已将项目删除并将其添加回列表视图中的操作已经完成并且可以正常工作很好.

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.

我的问题是,项目存储在sqlite数据库中,如何从表中删除项目? (我怎么知道用户还没有单击撤消按钮,所以我可以从数据库中完全删除数据.)

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).

这是 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;

因此,如果用户在小吃店的可见期间未单击撤消按钮,那么我需要从数据库中永久删除数据.

有什么解决办法吗?

推荐答案

据我所知,这是设计使然.您应该:

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

  • 用户点击删除按钮后立即删除项目;
  • 将其临时存储在类变量中;
  • 如果用户点击撤消",则将该项目再次添加到数据库中.

这种方法更安全,更强大;您不应该等待小吃店被解散,因为这种动作甚至都不会发生.试想一下,当小吃栏仍处于打开状态时,用户会强制退出该应用程序:是否应删除该项目?应该的.

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.

更值得信赖的消息来源是Ian Lake的g + post(由于G +弃用而被删除).在评论中,您可以阅读:

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

您希望您的UI立即做出反应(不要等到小吃店 消失)-大多数系统(尤其是与外部系统同步的系统) 服务器)具有软删除"的概念,其中将事物标记为 已删除.在这种情况下,撤消操作只会取消标记 记录为已删除.即使用户离开该系统也能正常工作 快餐栏完成之前的应用程序(您不能假定快餐栏 将始终完成其动画!).

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.

这篇关于在小吃店操作中,如何确定从数据库中永久删除软删除的记录是安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆