是否存在诸如“多路径推送"之类的问题?在Firebase实时数据库中? [英] Is There Such A Thing As "Multi-Path Push" in Firebase Real-Time Datasbase?

查看:59
本文介绍了是否存在诸如“多路径推送"之类的问题?在Firebase实时数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在观看此视频讨论Firebase中的多路径更新.多路径更新非常棒,因为它们使您可以一次或多次调用firebaseRef.update()方法,同时使它们成为一个原子的,全有或全无的操作.

I was watching this video which talks about multi-path updates in Firebase. Multi-path updates are great because they allow you to call the firebaseRef.update() method two or more times while having them be one atomic, all-or-nothing operation.

这很好,但是在我目前的情况下,我不想使用 update()方法.相反,我想使用FirebaseRef的 push 方法,并允许Firebase为我的对象生成一个唯一键.

This is great, but in my current situation I don't want to use the update() method. Instead, I want to use the FirebaseRef's push method and allow Firebase to generate a unique key for my object.

棘手的部分是我已对数据进行非规范化,因此它位于两个位置.我真正想做的是有一个原子的,要么全有要么全无的操作,该操作使用Firebase的 push()操作创建一个唯一键,然后用该键将对象保存在2个或更多不同的位置放置在我的数据库数据中.但是,push()的语法已经使用了一个对象,所以我什至想做些什么?

The tricky part is that I have denormalized my data so that it's in two places. What I would really like to do is have an atomic, all-or-nothing operation that uses Firebase's push() operation to create a unique key and then save the object with that key in 2 or more different places inside of my database data. However, the syntax for push() already uses an object so is what I want to do even possible?

注意:另一个可能的解决方案可能是使用Firebase api以某种方式在客户端中生成唯一密钥,然后使用该生成的密钥作为要插入的对象的密钥进行标准的多路径更新.

推荐答案

没有多位置推送,但是由于推送ID是在客户端上生成的,因此您可以使用多位置更新来执行所需的操作.

There is no multi-location push, but because push IDs are generated on the client, you can use a multi-location update to do what you want.

您可以通过调用不带参数的 push 来生成推送ID.推送ID是在客户端上生成的,生成一个ID不会与数据库进行交互:

You can generate a push ID by calling push with no arguments. Push IDs are generated on the client and generating one involves no interaction with the database:

let key = firebase.database().ref().push().key;

您也可以使用AngularFire2来执行此操作;不过,您必须将参数( undefined )传递给 push 才能安抚TypeScript:

You can use AngularFire2 to do this, too; although, you have to pass an argument (undefined) to push to appease TypeScript:

let key = angularFire.database.list('').push(undefined).key;

生成密钥后,您可以创建多位置更新:

Once you've generated the key, you can create a multi-location update:

let obj = { some: 'object' };
angularFire.database.object('').update({
    [`a/path/${key}`]: obj,
    [`another/path${key}`]: obj
});

此更新是原子性的,因此所有路径都将被更新,否则将不更新.

The update is atomic, so either all of the paths will be updated or none will.

这篇关于是否存在诸如“多路径推送"之类的问题?在Firebase实时数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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