每当我使用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);
答案 0 :(得分:1)
更新现有位置与添加新位置不同。
来自Firebase documentation on event types:
onChildAdded 事件...会针对每个现有子项触发一次,然后每次将新子项添加时再次触发到指定路径。
和
只要子节点已修改,就会触发 onChildChanged 事件。
由于您要添加新位置并修改现有位置,因此您必须实施onChildAdded()
和onChildChanged()
。