gpt4 book ai didi

java - 我的 onChildAdded() 在使用 Firebase Push() 时有效,但在更新时无效

转载 作者:行者123 更新时间:2023-12-01 11:07:15 27 4
gpt4 key购买 nike

每当我使用 updateChildren() 方法时,我的 onChildAdded() 方法似乎都不起作用。它与 push().setValue() 方法配合使用。这两种方法都可以将数据发送到 Firebase。有什么想法吗?

谢谢!

这是 push() 方法(有效)

private void saveToFirebase() {

String username = getIntent().getStringExtra("Username");

//takes string user input and creates a child of Gaben(parent)
Firebase usersRef = myFirebaseRef.child(username);

//sub child
Firebase location = usersRef.child("details");

Map mLocations = new HashMap();
mLocations.put("timestamp", mLastUpdateTime);
Map mCoordinate = new HashMap();

myFirebaseRef.push().setValue(mLocations);
}

private void drawLocations() {
// Get only latest logged locations - since 'START' button clicked
Query queryRef = myFirebaseRef.orderByChild("timestamp").startAt(startLoggingTime);
// Add listener for a child added at the data at this location
queryRef.addChildEventListener(new ChildEventListener() {
LatLngBounds bounds;
LatLngBounds.Builder builder = new LatLngBounds.Builder();

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map data = (Map) dataSnapshot.getValue();
String timestamp = (String) data.get("timestamp");
// Get recorded latitude and longitude
Map mCoordinate = (HashMap) data.get("location");

double latitude = (double) (mCoordinate.get("latitude"));
double longitude = (double) (mCoordinate.get("longitude"));

// Create LatLng for each locations
LatLng mLatlng = new LatLng(latitude, longitude);

// Make sure the map boundary contains the location
builder.include(mLatlng);
bounds = builder.build();

//get username input from login activity
String username = getIntent().getStringExtra("Username");

MarkerOptions mMarkerOption = new MarkerOptions()
// Add a marker for each logged location
.position(mLatlng)
.title(username)
.snippet(timestamp)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));

Marker nMarker = mMap.addMarker(mMarkerOption);
nMarker.showInfoWindow();

// Zoom map to the boundary that contains every logged location
mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, MAP_ZOOM_LEVEL));
}
});
}

这里的update()方法不起作用

private void saveToFirebase() {
String username = getIntent().getStringExtra("Username");

//takes string user input and creates a child of Gaben(parent)
Firebase usersRef = myFirebaseRef.child(username);

//sub child
Firebase location = usersRef.child("details");

Map mLocations = new HashMap();
mLocations.put("timestamp", mLastUpdateTime);
Map mCoordinate = new HashMap();

mCoordinate.put("latitude", mCurrentLocation.getLatitude());
mCoordinate.put("longitude", mCurrentLocation.getLongitude());

//updates location in firebase
location.updateChildren(mLocations);
location.updateChildren(mCoordinate);
}

基本上,唯一的区别是推送方法每次都会创建唯一的 ID,而另一种方法只是更新它。

唯一的区别是这些行

    location.updateChildren(mCoordinate);

还有这个

    myFirebaseRef.push().setValue(mLocations);

最佳答案

更新现有位置与添加新位置不同。

来自Firebase documentation on event types :

The onChildAdded event ... is triggered once for each existing child and then again every time a new child is added to the specified path.

还有:

The onChildChanged event is triggered any time a child node is modified.

由于您要添加新位置并修改现有位置,因此必须同时实现 onChildAdded()onChildChanged()

关于java - 我的 onChildAdded() 在使用 Firebase Push() 时有效,但在更新时无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32811564/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com