Firebase: updating multiple objects in a single transaction

I found several threads below which are described about this scenario.

However, it is still difficult to find if there is any recommended data structure design for this. I see the multitasking firebase-multi-write library which says that probably in most cases this is not needed.

But I think I need it, my scenario: 3 users

/users/A : {credit:20} /users/B : {credit:3} /users/C : {credit:10} 

And each user can steal loans from each other all at the same time .,

  • Colors from B-loans look like {21, 2}
  • B steals from C credits as {3, 9}
  • C steals from loans A, like {11, 20}

Now I need to update credits for each user in order to maintain this consistency, so that every theft operation is atomic in nature.

What would be the best way to deal with such a scenario while maintaining data integrity in Java ?

+6
source share
1 answer

In September / 2015, firebase developers released a new version .

In this release, you can do this in one atomic update in both locations:

 Firebase ref = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com"); // Generate a new push ID for the new post Firebase newPostRef = ref.child("posts").push(); String newPostKey = newPostRef.getKey(); // Create the data we want to update Map newPost = new HashMap(); newPost.put("title", "New Post"); newPost.put("content", "Here is my new post!"); Map updatedUserData = new HashMap(); updatedUserData.put("users/posts/" + newPostKey, true); updatedUserData.put("posts/" + newPostKey, newPost); // Do a deep-path update ref.updateChildren(updatedUserData, new Firebase.CompletionListener() { @Override public void onComplete(FirebaseError firebaseError, Firebase firebase) { if (firebaseError != null) { System.out.println("Error updating data: " + firebaseError.getMessage()); } } }); 
+3
source

All Articles