我发现事务(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/