Subratsss
4/7/2018 - 7:59 AM

Firestore transaction


//transaction
 /**
     * For tag Group into Customers info of vendorCustomer
     * @param storeID
     * @param groupName
     * @param dataOperationCompletionListener
     */

 @Override
    public void tagGroupIntoCustomers(String storeID, String customerDocId,final String groupName, final DataOperationCompletionListener dataOperationCompletionListener) {
        //Create reference of Customers
        final DocumentReference customerInfoReference = vendorsCustomerCollection.document(storeID).collection(BillConstants.CUSTOMERS).document(customerDocId);

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

                DocumentSnapshot documentSnapshot = transaction.get(customerInfoReference);
                CustomerModel customerModel = documentSnapshot.toObject(CustomerModel.class);
                customerModel.setTaggedGroups(groupName);

                transaction.update(customerInfoReference,BillConstants.TAG_GROUP,groupName);

                return null;
            }
        }).addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                dataOperationCompletionListener.operationCompleted(true);

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

            }
        });


    }