Android Studio Firebase updateChildren

时间:2018-08-07 00:15:55

标签: java android firebase firebase-realtime-database

大家好。我在Android Studio和Firebase中度过了艰难的时光,每当我尝试使用updateChildren时,它都会继续替换旧的。我以为它将旧数据保留在节点内,这是代码:

private void getClosestDriver() {
    DatabaseReference driverLocation = FirebaseDatabase.getInstance().getReference().child("driversAvailable");
    GeoFire geoFire = new GeoFire(driverLocation);

    geoQuery = geoFire.queryAtLocation(new GeoLocation(pickupLocation.latitude, pickupLocation.longitude), radius);

    geoQuery.removeAllListeners();

    geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
        @Override
        public void onKeyEntered(String key, GeoLocation location) {
            if (!driverFound && requestBol) {
                driverFound = true;
                driverFoundID = key;

                DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child("Driver").child(driverFoundID).child("passengerRequest");
                String passengerID = FirebaseAuth.getInstance().getCurrentUser().getUid();

                HashMap map = new HashMap();
                map.put("passengerRideID", passengerID);
                map.put("destination", destination);
                ref.updateChildren(map);

                getDriverLocation();
                mRequest.setText("Looking for Bus Location....");
            }
        }

我对HasMap做正确的事情吗?

1 个答案:

答案 0 :(得分:0)

通过将其更改为

String passengerID = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child("Driver").child(driverFoundID).child("passengerRequest").child(passengerID);


            HashMap map = new HashMap();
            map.put("destination", destination);
            ref.updateChildren(map);