【问题标题】:How to update different node child at same time firebase android如何同时更新不同的节点子节点firebase android
【发布时间】:2020-01-07 16:54:09
【问题描述】:

我想根据条件更新时间段,例如当用户预订自行车开始时间上午 9:00 和结束时间下午 12:00 时,我想更新时间段从上午 9:00 到上午 12:00 可用性 false。 那么是否可以同时更新不同的孩子。或者请给我一条路径,我该怎么做。

                DatabaseReference ref= FirebaseDatabase.getInstance().getReference().child("TimeSlot").child(Common.BikeId);

            ref.orderByChild("TimeSlot").equalTo("10:00AM").addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                   //update children


                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });

在上面的代码中,我一次只能更新一个孩子 如何解决这种解决方案。

【问题讨论】:

标签: android firebase firebase-realtime-database


【解决方案1】:

首先,您无法将StringstartAtendAt 时间进行比较。而是使用像 [0 - 24] 这样的 long 值来保持 24 小时格式的时间。

然后,执行下面的操作将子isAvailable更新为false

DatabaseReference ref = FirebaseDatabase.getInstance().getReference()
        .child("TimeSlot")
        .child(Common.BikeId);

ref.orderByChild("time_slot")
        .startAt(9.0)
        .endAt(12.0)
        .addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for(DataSnapshot childSnapshot: dataSnapshot.getChildren()) {

                    //Check whether child with key 'isAvailable' exist or not
                    if(childSnapshot.hasChild("isAvailable")) {

                        //Now update the status with false
                        ref.child(childSnapshot.getKey())
                                .child("isAvailable")
                                .setValue(false);
                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

【讨论】:

  • 非常感谢。你的解决方案就像一个魅力。在这个问题上花了很多时间。你的解决方案真的很完美......再次感谢@Md Asaduzzaman
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-08
  • 1970-01-01
相关资源
最近更新 更多