user11093104
user11093104

Reputation:

How to run the multiple transactions in Firestore database? I am facing issue in that

I am trying to run the multiple transactions in firestore database in android. but they are not working all at one time. only transaction is working right now that is of DocumentReference PostRef.

please assist in running multiple transactions.

db.runTransaction(new Transaction.Function<Void>() {
    @Override
    public Void apply(Transaction transaction) throws FirebaseFirestoreException {

       DocumentSnapshot documentSnapshot1 = transaction.get(PostRef);
       boolean l2 = documentSnapshot1.getBoolean("l2");
        if(l2 == false) {
            transaction.update(PostRef, "l2", true);
            return null;

        }

        DocumentSnapshot documentSnapshot2 = transaction.get(PostUserRef);
        long l11 = documentSnapshot2.getLong("l1");
        { transaction.update(PostRef, "l1", l11+1);

        }


        DocumentSnapshot snapshot = transaction.get(likesRef);
        boolean l1 = snapshot.getBoolean("l1");
        if (l1 == false) {

            transaction.update(likesRef, "l1", true);
            //2 transactions to update userprofile



            return null;
        } else
        {
            throw new FirebaseFirestoreException("You already liked",
                    FirebaseFirestoreException.Code.ABORTED);
        }


    }
})

Upvotes: 1

Views: 292

Answers (1)

user11093104
user11093104

Reputation:

What I did is I Setup the multiple transactions as a function and then called those functions in the onclick event and it performed the required functions

Upvotes: 2

Related Questions