【发布时间】:2019-08-17 06:40:31
【问题描述】:
问题总结:我是 Android Studio 和 Firebase 与 Geofire 的新手,请温柔。我的目标是当用户在我的应用程序的地图屏幕上没有 Geofire 位置时将其注销。我不知道如何在我的 Geofire 中查询“g”。如何查询“g”子项以查看它是否已创建?如果未创建“g”,则需要注销它们。所以目标是查询“g”并创建方法,如果“g”为空,则将用户注销。
我做过的事情: 许多 Stackoverflow 帖子都没有使用 Android Studio。我确实找到了这个I have inserted location using geofire in firebase,但它并没有解决我的问题。我也试过Google firebase check if child exists
onLocationChanges 是我添加在线供应商的位置,我还想检查是否创建了“g”。
@Override
public void onLocationChanged(Location location) {
// Adds VendorOnline Child to Firebase when Vendor is On This Activity
addVendorOnline();
// Log user out if they are on the map screen without a "VendorOnline" Uid
logVendorOutIfBuggy();
}
这是创建在线供应商并检查“g”是否已创建的方法:
private void addVendorOnline(){
if (FirebaseAuth.getInstance().getCurrentUser() != null) {
String vendorId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference vendorIdReference = FirebaseDatabase.getInstance().getReference("VendorOnline");
GeoFire vendorGeoFire = new GeoFire(vendorIdReference);
vendorGeoFire.setLocation(vendorId, new GeoLocation(lastLocation.getLatitude(), lastLocation.getLongitude()));
}
}
// Log user out if for some reason they do not have a "g" child but are on the map screen.
private void logVendorOutIfBuggy() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("g");
if(ref == null){
Toast.makeText(VendorMapsActivity.this, "Error", Toast.LENGTH_LONG).show();
FirebaseAuth.getInstance().signOut();
}
我预期会发生什么以及实际会发生什么:如果“g”等于 null,我希望用户退出,但现在用户不会退出。
【问题讨论】:
-
g属性在您将位置写入密钥时由 Geofire 自动创建/更新。只要您使用 Geofire,您就不必专门为该属性做任何事情。这听起来有点像XY problem 这里。您想通过检测g创建来完成什么? -
如果没有检测到“g”,我正在尝试注销用户。
-
我会调用
getLocation作为供应商密钥,如果从未设置(或删除)位置,LocationCallback应该在onLocationCallback中返回空值。如果有 'l' 值而没有 'g' 也将是不一致的,因此在没有位置推断出没有 'g' 是公平的。 -
太棒了,这听起来很有希望。你能给出一个示例代码吗?谢谢朋友。
标签: android firebase-realtime-database geofire