Firebase事务API调用当前数据为空 [英] Firebase transaction api call current data is null

查看:61
本文介绍了Firebase事务API调用当前数据为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 transaction()更新位置时,即使该位置包含一些数据,该位置的数据也会返回空值.

我在同一位置读取数据后尝试transaction() ,当时它在该位置提供了所有数据.

如果是上述情况,如何使用transaction()?

解决方案

事务以Amazon SimpleDB或数据库分片群集的方式工作.也就是说,它们最终是一致的",而不是保证一致的.

因此,当您使用事务时,处理函数可能会多次调用一个本地值(在某些情况下,如果从未检索过,则返回null),然后再次使用同步值(无论服务器上是什么). /p>

示例:

pathRef.transaction(function(curValue) {

    // this part is eventually consistent and may be called several times

}, function(error, committed, ss) {

    // this part is guaranteed consistent and will match the final value set

});

这实际上是您无论如何都要进行交易的心态.您应该始终期望有多个调用,因为第一个事务可能会与另一个更改发生冲突并被拒绝.您不能使用事务的处理方法来获取服务器值(尽管您可以从成功回调中读取它).

防止本地触发的事件

发生事务时,将在本地事件到达服务器以进行延迟补偿之前触发该事件.如果事务失败,则将还原本地事件(触发更改或删除事件).

您可以在交易中使用applyLocally属性 覆盖这种行为会使本地结果变慢,但可以确保仅在本地触发服务器值.

pathRef.transaction(function(curValue) {

    // this is still called multiple times

}, function(error, committed, ss) {

    // this part is guaranteed consistent and will match the final value set

}, 
    // by providing a third argument of `true`, no local event
    // is generated with the locally cached value.
true);

When I use transaction() to update a location, data at that location is returning null even though the location having some data.

I tried transaction() after reading data at the same location that time it is giving all data at that location.

How can I use transaction() if the case is like the above?

解决方案

Transactions work in the manner of Amazon's SimpleDB or a sharded cluster of databases. That is to say, they are "eventually consistent" rather than guaranteed consistent.

So when you are using transactions, the processing function may get called more than once with a local value (in some cases null if it's never been retrieved) and then again with the synced value (whatever is on the server).

Example:

pathRef.transaction(function(curValue) {

    // this part is eventually consistent and may be called several times

}, function(error, committed, ss) {

    // this part is guaranteed consistent and will match the final value set

});

This is really the mindset with which you must approach transaction anyways. You should always expect multiple calls, since the first transaction may collide with another change and be rejected. You can't use a transaction's processing method to fetch the server value (although you could read it out of the success callback).

Preventing the locally triggered event

When the transaction happens, a local event is triggered before it reaches the server for latency compensation. If the transaction fails, then the local event will be reverted (a change or remove event is triggered).

You can use the applyLocally property on transactions to override this behavior, which makes the local results slower but ensures that only the server value is triggered locally.

pathRef.transaction(function(curValue) {

    // this is still called multiple times

}, function(error, committed, ss) {

    // this part is guaranteed consistent and will match the final value set

}, 
    // by providing a third argument of `true`, no local event
    // is generated with the locally cached value.
true);

这篇关于Firebase事务API调用当前数据为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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