似乎每当我使用onChildAdded()
方法时,我的updateChildren()
方法都不起作用。它与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);
}
基本上,唯一的区别是push方法每次都会创建唯一的ID,而其他方法只是对其进行更新。
唯一的区别是这些行
location.updateChildren(mCoordinate);
和这个
myFirebaseRef.push().setValue(mLocations);
最佳答案
更新现有位置与添加新位置不同。
从Firebase documentation on event types:
对于每个现有子项,都会触发一次onChildAdded事件...,然后每次将新子项添加到指定路径时,都会再次触发该事件。
和:
每当修改子节点时,都会触发onChildChanged事件。
由于要添加新位置并修改现有位置,因此必须同时实现onChildAdded()
和onChildChanged()
。
关于java - 我的onChildAdded()在使用Firebase push()而不是更新时可以工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32811564/