1

in order to have atomic data operations in my Firebase Android applications, I'm using the updateChildren(Map<String, Object> update) method to set multiple values with a single command (this prevents data inconsistencies.

In some cases, upon writing data I also need to set priorities in order for the index elements to be sorted correctly.

Using the 'not atomic' setValue(Object value, Object priority) operation, it is possible to write a value and set the priority at the same time. Using the updateChildren() commmand does not allow to set priorities.

In order to set the priorities of newly created data, I currently call setPriority() in the CompletionListener of the updateChildren() command. This works, but causes strange UI behavior (elements are added to the bottom of the list, and after this they move to the correct place).

What would be the recommended way to handle this correctly?

Peter
  • 10,910
  • 3
  • 35
  • 68

1 Answers1

2

If you're looking to include a priority in a call to updateChildren, you can do so by having a key that ends in .priority:

map.put("path/to/child/.priority", 42);

In general though, there is no reason to still use priorities. See What does priority mean in Firebase?

Community
  • 1
  • 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Hi Frank, thanks for your quick reaction, I have extended the question with more details. Please have a look. – Peter Sep 17 '16 at 20:01
  • The code I posted shows how you can update an item's priority within an `updateChildren()` call. While I can definitely help further on replacing the priority with either a `orderByValue()` or `orderByChild()`, it's probably a separate question. There: post complete information, because your current way of asking makes it hard to help beyond what I've already done. Provide the minimal JSON (as text, not a screenshot) that you're trying to load or manipulate and the code snippet of how you're trying to load or manipulate it. – Frank van Puffelen Sep 18 '16 at 01:13
  • I have created a new question dedicated to this topic: [How to use orderByChild or orderByValue in a FirebaseRecyclerAdapter](http://stackoverflow.com/questions/39561612/how-to-use-orderbychild-or-orderbyvalue-in-a-firebaserecycleradapter) – Peter Sep 18 '16 at 19:15