도움이 되었습니까?

해결책

업데이트 카운터를 활용하여 트랜잭션과 비슷한 방식으로 작성 작품을 작동시킬 수 있습니다.(아래의 의사 코드를 사용할 것입니다. 죄송합니다. 그러나 예제를 위해 전체 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);
   }
}
.

다른 팁

FYI, Firebase 실시간 데이터베이스는 공식적 으로이 작업을 지원합니다.

블로그 docs 자세한 정보

Firebase-transactions 프로젝트를 확인하십시오 : https://github.com/vacuumlabs/firebase-transactions

나는 당신이 당신이 서버에서 많은 쓰기를하는 경우 특히이 사건에 상당히 편리 할 수 있다고 믿습니다.

(Disclaimer : 저는 저자 중 하나)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top