Peter Peter - 2 months ago 25
Android Question

Setting priorities in a DatabaseReference.updateChildren() call?

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?

Answer

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?

Comments