有没有“多路径推送"这样的东西?在 Firebase 实时数据库中? [英] Is There Such A Thing As "Multi-Path Push" in Firebase Real-Time Datasbase?

查看:27
本文介绍了有没有“多路径推送"这样的东西?在 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 是在客户端生成的,生成一个不涉及与数据库的交互:

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天全站免登陆