Android – if previously committed locally, firebase transactions will not be aborted remotely

I am building an Android Application in which several users can access, modify and delete the same item. I am using firebase to synchronize all devices. In order to track updates, the item has a timestamp. I write a transaction so that when I try to delete the item, it will check whether the timestamp of my copy is older than that of the remote copy: in this case, The transaction is aborted and the item is not deleted

This is my question:

>My device goes offline > I successfully deleted the item > another user modifies the item on the remote database > My device goes online and propagates his deletion

I think it will abort remotely because of the remote timestamp update. If I can only make a decision based on my local data, I really can't see the significance of the abort function

How should I handle these conflicts in firebase?

– update

This is the code I used to delete the project. If another user changes the project remotely after local deletion, it should be aborted

private void removeItem(final ListItem item, final Firebase itemRef) {
    itemRef.runTransaction(new Transaction.Handler() {
        @Override
        public Transaction.Result doTransaction(MutableData mutableData) {
            if(mutableData == null) return Transaction.abort();
            else if((long)mutableData.child("lastUpdate").getValue() > item.getLastUpdate()) return Transaction.abort();
            else{
                itemRef.removeValue();
                return Transaction.success(mutableData);
            }
        }
    });

Note that I use itemref. Removevalue () instead of mutabledata. SetValue (null), because the second one doesn't seem to work

resolvent:

Firebase initially applies client transactions to improve concurrency. If the decision does not conform to your use case, you can pass another parameter to the transaction method, which tells it to bypass the local apply

see https://www.firebase.com/docs/web/api/firebase/transaction.html

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>