Firebase:从存储和数据库中删除时,存储删除是否应该在存储删除的onSuccessListener中? [英] Firebase: When deleting from storage AND database, should the storage deletion be in the onSuccessListener of the storage deletion?

查看:136
本文介绍了Firebase:从存储和数据库中删除时,存储删除是否应该在存储删除的onSuccessListener中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我要删除Firebase存储中的条目以及Firebase数据库中的元数据。我是否应该像这样彼此独立地删除它们:

let's say I want to delete an entry from the Firebase Storage and also the meta data from the Firebase Database. Should I then delete them independently from each other like this:

@Override
public void onDeleteClick(int position) {
    Upload selectedItem = mUploads.get(position);
    String selectedKey = selectedItem.getKey();

    StorageReference imageRef = FirebaseStorage.getInstance().getReferenceFromUrl(selectedItem.getImageUrl());
    imageRef.delete();

    mDatabaseRef.child(selectedKey).removeValue();
}

或者我应该将数据库删除部分放入存储删除方法的onSuccessListener中?

Or should I put the database deletion part into the onSuccessListener of the storage delete method?

@Override
public void onDeleteClick(int position) {
    Upload selectedItem = mUploads.get(position);
    final String selectedKey = selectedItem.getKey();

    StorageReference imageRef = FirebaseStorage.getInstance().getReferenceFromUrl(selectedItem.getImageUrl());
    imageRef.delete().addOnSuccessListener(new OnSuccessListener<Void>() {
        @Override
        public void onSuccess(Void aVoid) {
            mDatabaseRef.child(selectedKey).removeValue();
        }
    });

    mDatabaseRef.child(selectedKey).removeValue();
}


推荐答案

每当你需要删除一个项目和跨多个服务的引用,您运行一个操作完成而另一个操作失败的机会。此导致数据损坏,因此您需要弄清楚对您的应用程序最不利的内容:

Whenever you need to delete an item and a reference across multiple services, you run the chance that one operation completes and the other fails. This will lead to corrupted data, so you'll need to figure out what is least harmful for your app:


  1. 有一个孤立的图像,没有数据库节点引用它。

  2. 有一个指向不再存在的图像的数据库属性。

如果孤立图像对应用程序的危害较小,请先删除数据库节点,然后在完成处理程序中删除该图像。这是我的首选选项,因为我的客户端应用程序代码不需要对孤立图像进行任何特殊处理。

If the orphaned image is the less harmful option for your app, delete the database node first and then delete the image in the completion handler. This is my preferred option, since my client application code does not need any special handling for the orphaned images.

如果数据库中悬挂引用的危害最小,请删除首先是文件,然后在Peter回答时更新数据库。

If having a dangling reference in the database is least harmful, delete the file first and then update the database as Peter answered.

无论您选择哪个选项,您通常都希望定期运行维护过程来清理悬挂的引用。数据库和/或存储中的孤立文件。

Whichever option you choose, you'll typically want to periodically run a maintenance process that cleans up the dangling references in the database, and/or the orphaned files in storage.

这篇关于Firebase:从存储和数据库中删除时,存储删除是否应该在存储删除的onSuccessListener中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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