Firestore-Android Java-Transaction

Firestore-Android Java-Transaction,java,android,google-cloud-firestore,transactions,Java,Android,Google Cloud Firestore,Transactions,我想知道如何停止Firestore中的交易操作。函数重试多次,但如果满足条件,则不需要执行此操作。例如,在线文档提供的示例: 如果newPopulation>1000000,将重试多次此块。我想做的是: 如果newPopulation>1000000,则退出事务操作(不再重试) 我的计划允许客户在轮班的情况下进行预订。每班的插槽数量为4个 if (shiftQuantity == quantityTotalBookingByShift) {

我想知道如何停止Firestore中的交易操作。函数重试多次,但如果满足条件,则不需要执行此操作。例如,在线文档提供的示例:

如果newPopulation>1000000,将重试多次此块。我想做的是:

如果newPopulation>1000000,则退出事务操作(不再重试)

我的计划允许客户在轮班的情况下进行预订。每班的插槽数量为4个

                if (shiftQuantity == quantityTotalBookingByShift) {

                    throw new FirebaseFirestoreException(errorMsg1 + shift.toString() + errorMsg2,
                    FirebaseFirestoreException.Code.ABORTED);

                }
上面的代码检查用户选择的班次中是否有4个插槽,事务中止。所以,这就是我问题的原因。 我的代码如下

   public void createBooking(final Booking booking, final Integer quantityTotalBookingByShift, final FirebaseListener listener) {

    final DocumentReference counterDocRef = db.collection(bookingsCollectionPath).document(counterDocument);

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd.MM.YYYY");

    final String dateAux = formatter.format(booking.getDate());

    final DocumentReference dateRef = db.collection(bookingByDateCollectionPath).document(dateAux);

    db.runTransaction(new Transaction.Function<Integer>() {


        @Nullable
        @Override
        public Integer apply(@NonNull Transaction transaction) throws FirebaseFirestoreException {

            final String idBookingCounter = "idBooking";
            final String listOfBookings = "ListOfBookings";

            // Read
            DocumentSnapshot counterSnapshot = transaction.get(counterDocRef);

            Integer counter = Integer.parseInt(String.valueOf(counterSnapshot.getLong(idBookingCounter)));

            counter++;

            booking.setId(counter);

            DocumentSnapshot dateSnapshot = transaction.get(dateRef);

            Integer shiftQuantity;

            for (Shift shift: booking.getShifts()) {

                shiftQuantity = Integer.parseInt(String.valueOf(dateSnapshot.getLong(shift.getId().toString())));

                if (shiftQuantity == quantityTotalBookingByShift) {

                    throw new FirebaseFirestoreException(errorMsg1 + shift.toString() + errorMsg2,
                    FirebaseFirestoreException.Code.ABORTED);

                }

            }
            // Write counter
            transaction.update(counterDocRef, idBookingCounter, counter);

            //write increment shift booking
            for (Shift shift: booking.getShifts()) {

                shiftQuantity = Integer.parseInt(String.valueOf(dateSnapshot.getLong(shift.getId().toString()))) + 1;

                transaction.update(dateRef, shift.getId().toString(), shiftQuantity);

            }

            // write bookings
            DocumentReference bookingsDocRef = db.collection(bookingsCollectionPath).document(booking.getId().toString());


            Map<String, Object> bookingToSave = createDataBookingToSave(booking);

            transaction.set(bookingsDocRef, bookingToSave);

            // write booking inside the user subtree
            transaction.set(db.collection(bookingByUserCollectionPath).document(booking.getUser().getId()).collection(listOfBookings).document(booking.getId().toString()), bookingToSave);

            return counter;

        }

        private Map<String, Object> createDataBookingToSave(Booking booking) {


            Map<String, Object> docDataBooking = new HashMap<>();

            docDataBooking.put("id", booking.getId());
            docDataBooking.put("date", new Timestamp(java.sql.Date.valueOf(booking.getDate().toString())));
            docDataBooking.put("createdAt", FieldValue.serverTimestamp());
            docDataBooking.put("type", booking.getType());
            docDataBooking.put("comments", booking.getComments());
            docDataBooking.put("vehicle", booking.getVehicle().getNumberPlate());
            docDataBooking.put("status", booking.getStatus());
            docDataBooking.put("shifts", booking.getShifts());
            docDataBooking.put("user", booking.getUser().getId());

            return docDataBooking;

        }
    }).addOnSuccessListener(new OnSuccessListener<Integer>() {
        @Override
        public void onSuccess(Integer aLong) {
                listener.onSuccess(aLong);

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

}
public void createBooking(最终预订预订、最终整数数量totalbookingbhift、最终FirebaseListener侦听器){
最终文档参考counterDocRef=db.collection(bookingsCollectionPath)。文档(counterDocument);
DateTimeFormatter formatter=模式的DateTimeFormatter.of(“dd.MM.YYYY”);
最终字符串dateAux=formatter.format(booking.getDate());
最终文档引用dateRef=db.collection(bookingByDateCollectionPath)。文档(dateAux);
db.runTransaction(new Transaction.Function(){
@可空
@凌驾
公共整数应用(@NonNull事务处理)引发FirebaseFirestoreException{
最后一个字符串idBookingCounter=“idBooking”;
最终字符串listOfBookings=“listOfBookings”;
//阅读
DocumentSnapshot counterSnapshot=事务.get(counterDocRef);
整数计数器=Integer.parseInt(String.valueOf(counterSnapshot.getLong(idBookingCounter));
计数器++;
预订。setId(柜台);
DocumentSnapshot dateSnapshot=transaction.get(dateRef);
整数移位量;
for(班次:booking.getShifts()){
shiftQuantity=Integer.parseInt(String.valueOf(dateSnapshot.getLong(shift.getId().toString()));
if(shiftQuantity==数量TotalBookingByShift){
抛出新的FirebaseFirestoreException(errorMsg1+shift.toString()+errorMsg2,
FirebaseFirestoreException.Code.已中止);
}
}
//写计数器
事务处理更新(counterDocRef、idBookingCounter、counter);
//写增量轮班票
for(班次:booking.getShifts()){
shiftQuantity=Integer.parseInt(String.valueOf(dateSnapshot.getLong(shift.getId().toString()))+1;
transaction.update(dateRef,shift.getId().toString(),shiftQuantity);
}
//写预订
DocumentReference bookingsDocRef=db.collection(bookingsCollectionPath).document(booking.getId().toString());
Map bookingToSave=createDataBookingToSave(预订);
事务.set(bookingsDocRef、bookingstosave);
//在用户子树中写入预订
事务.set(db.collection(bookingByUserCollectionPath).文档(booking.getUser().getId()).集合(图书列表).文档(booking.getId().toString()),bookingToSave);
返回计数器;
}
私人地图createDataBookingToSave(预订){
Map docDataBooking=newhashmap();
docDataBooking.put(“id”,booking.getId());
docDataBooking.put(“日期”),新的时间戳(java.sql.date.valueOf(booking.getDate().toString());
put(“createdAt”,FieldValue.serverTimestamp());
docDataBooking.put(“type”,booking.getType());
docDataBooking.put(“comments”,booking.getComments());
docDataBooking.put(“车辆”,booking.getVehicle().getNumberPlate());
docDataBooking.put(“status”,booking.getStatus());
docDataBooking.put(“班次”,booking.getShifts());
docDataBooking.put(“用户”,booking.getUser().getId());
返回docDataBooking;
}
}).addOnSuccessListener(新的OnSuccessListener(){
@凌驾
成功时的公共void(沿整数){
listener.onSuccess(沿);
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常e){
onFailure(新的FirebaseException(e.getMessage());
}
});
}

请分享您尝试过的代码。提供示例,这是非常少的原始信息。“寻求调试帮助的问题('为什么此代码不起作用?'))必须包括所需的行为、特定的问题或错误,以及在问题本身中重现问题所需的最短代码。没有明确问题说明的问题对其他读者没有用处。请参阅: