rest - 通过REST API进行Firebase交易

标签 rest firebase

我发现事务(https://www.firebase.com/docs/transactions.html)是处理并发的一种很酷的方法,但是似乎只能从客户端完成。

我们使用Firebase的方式主要是通过从服务器写入数据并在客户端上观察数据。通过REST API写入数据时,是否有一种方法可以实现乐观并发模型?

谢谢!

最佳答案

您可以利用更新计数器使写操作的工作方式与事务类似。 (我将在下面使用一些伪代码;对此感到抱歉,但是我不想写出完整的REST API作为示例。)

例如,如果我有一个这样的对象:

{
   total: 100,
   update_counter: 0
}

和这样的写规则:
{
   ".write": "newData.hasChild('update_counter')",
   "update_counter": {
      ".validate": "newData.val() === data.val()+1"
   }
}

现在,我可以通过在每个操作中简单地传入update_counter来防止并发修改。例如:
var url = 'https://<INSTANCE>.firebaseio.com/path/to/data.json';
addToTotal(url, 25, function(data) {
   console.log('new total is '+data.total);
});

function addToTotal(url, amount, next) {
   getCurrentValue(url, function(in) {
      var data = { total: in.total+amount, update_counter: in.update_counter+1 };
      setCurrentValue(ref, data, next, addToTotal.bind(null, ref, amount, next));
   });
}

function getCurrentValue(url, next) {
   // var data = (results of GET request to the URL) 
   next( data );
}

function setCurrentValue(url, data, next, retryMethod) {
   // set the data with a PUT request to the URL
   // if the PUT fails with 403 (permission denied) then
   // we assume there was a concurrent edit and we need
   // to try our pseudo-transaction again
   // we have to make some assumptions that permission_denied does not
   // occur for any other reasons, so we might want some extra checking, fallbacks,
   // or a max number of retries here
   // var statusCode = (server's response code to PUT request)
   if( statusCode === 403 ) {
       retryMethod();
   }
   else {
       next(data);
   }
}

关于rest - 通过REST API进行Firebase交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23041800/

相关文章:

mysql - 使用用户搜索参数生成 MySQL JSON_EXTRACT 条件

android - 当 firebase 数据库发生更改而不使用控制台或第 3 方服务器时,是否可以使用 FCM 发送推送通知?

javascript - Firebase 未加载到 Chrome 扩展内容脚本中

java - 通过休息传输大量数据

javascript - Firestore 云功能无法在本地运行

ios - 如何知道在 swift 4 中实时 firebase 的 childChanged 事件类型中更新了哪个子子

firebase - 一次关闭 Crashlytics 中的所有未解决问题?

api - Instagram API 速率限制 - 已签名的 POST 调用 - 仍被视为未签名的调用

java - 在java中运行Restful Web Service时出现404错误

java - 如果底层数据可能发生变化,如何管理超媒体驱动的 RESTful API 的分页结果?