跨多个节点的Firebase数据一致性 [英] Firebase data consistency across multiple nodes

查看:114
本文介绍了跨多个节点的Firebase数据一致性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据火力点文档的数据是平坦的和索引用于链接树中的不同节点:$ b​​
$ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
$ $ widgetId
widgets
$ widgetId

在上面的例子中,如果一个用户创建一个小部件, widgetid 也存储在用户节点下。



我的问题是有没有办法保证操作的一致性,考虑到现在有多个写需要。

假设第一个操作是:
$ b $ {p var newKey = fb.child('widgets')。push({name:'widge'}).key();



然后我可以把它写到:

$ p $ fb.child('users')。child(auth .id).child('widgets')。child(newKey).set(true);



但是如果发生故障或其他问题他们之间的两个写道?或者,如果我有多个地方我需要存储该密钥,并在这些写入之间发生故障?



目前有一种方法来处理这个在firebase?如果没有,是否有计划在未来支持这一点?



如果是这样,有人可以提供一个具体的例子是如何做到的?

解决方案

请注意,这是Firebase核心API的一部分。有关详细信息,请参阅此博客文章

  var mergedUpdate = {}; 
mergedUpdate ['users /'+ userId +'/ widgets /'+ widgetId] = true;
mergedUpdate ['widgets /'+ widgetId] = widgetData;

var ref = new Firebase(https://< YOUR-FIREBASE-APP> .firebaseio.com /);
ref.update(mergedUpdate);


According to the firebase docs data is flattened and indices are used to link different nodes in the tree:

users
  $userId
    widgets
      $widgetId
widgets
  $widgetId

In the above example, if a user creates a widget, that widgetid is also stored under the user node.

My question is whether there is a way to guarantee the consistency of the operation, considering there is now the more than one write required.

Assuming the first operation is:

var newKey = fb.child('widgets').push({ name: 'widge' }).key();

I can then write it to:

fb.child('users').child(auth.id).child('widgets').child(newKey).set(true);

But what if there was a failure or other problem between the two writes? Or if I have multiple places I need to store that key and a failure occurs between those writes?

Is there currently a way to handle this in firebase?

If not, are there plans to support this in the future?

And if so, can someone provide a specific example of how that would be done?

解决方案

Note that this is now part of the core Firebase API. See this blog post for details.

var mergedUpdate = {};
mergedUpdate[ 'users/' + userId + '/widgets/' + widgetId ] = true;
mergedUpdate[ 'widgets/' + widgetId ] = widgetData;

var ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
ref.update(mergedUpdate);

这篇关于跨多个节点的Firebase数据一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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