如何在Firebase存储中使用WriteBatch?

huangapple go评论71阅读模式
英文:

How to use WriteBatch with Firebase storage?

问题

我正在使用Firebase云数据库和Firebase存储。我正在使用以下代码来使用WriteBatch删除两个文档:

WriteBatch batch = fireDB.batch();
batch.delete(docRef1);
batch.delete(docRef2);
batch.commit().addOnCompleteListener(new OnCompleteListener<Void>() {

	@Override
	public void onComplete(@NonNull Task<Void> task) {
		// 代码
	}
});

我还想使用WriteBatch从存储中删除图像,这意味着如果其中一个操作失败,它们都会失败。我有一个名为imageURL的变量,其中包含Firebase存储中图像的URL。我尝试了以下代码:

batch.delete(storage.getStorage().getReferenceFromUrl(imageURL));

但它不起作用,因为:

'com.google.firebase.firestore.WriteBatch' 中的 'delete(com.google.firebase.firestore.DocumentReference)' 无法应用于 '(com.google.firebase.storage.StorageReference)'

是否可能实现这个目标?

英文:

I'm using Firebase cloud database and firebase storage. I'm using the following code in order to remove two documents using WriteBatch:

WriteBatch batch = fireDB.batch();
batch.delete(docRef1);
batch.delete(docRef2);
batch.commit().addOnCompleteListener(new OnCompleteListener&lt;Void&gt;() {

	@Override
	public void onComplete(@NonNull Task&lt;Void&gt; task) {
		// code
	}
});

I want also to remove the image from the storage using WriteBatch, meaning, if one of them fails, they all fail to remove. I have a variable called imageURL which contains the URL of the image in the firebase storage. I tried:

batch.delete(storage.getStorage().getReferenceFromUrl(imageURL));

But it does not work because:

> 'delete(com.google.firebase.firestore.DocumentReference)' in 'com.google.firebase.firestore.WriteBatch' cannot be applied to '(com.google.firebase.storage.StorageReference)'

Is it possible to do?

答案1

得分: 2

没有办法在多个Firebase产品上运行单个操作。

最好的方法是:

  • 按照对你的用例造成最少问题的顺序执行删除操作。通常情况下,这意味着最后删除图像,因为拥有一个孤立的图像对应用程序的影响较小,而拥有指向不存在图像的悬挂引用则会更加混乱。
  • 为读取编写健壮的代码,能够处理孤立的图像和悬挂引用。
  • 定期进行清理,通常在云函数中执行,清除孤立的文件和悬挂引用。
英文:

There is no way to run a single operation across multiple Firebase products.

The best you can do is:

  • Perform the deletes in the order that leads to the least problems for your use-case. Typically this means deleting the images last, as having an orphaned image is less disruptive for an app than having a dangling reference to a non-existing image.
  • Write robust code for the reading, that can deal with both orphaned images, and dangling references.
  • Perform periodic cleanup, typically in a Cloud Function, getting rid of both orphaned files and dangling references.

huangapple
  • 本文由 发表于 2020年8月7日 07:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63292969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定