android - 如果没有创建 Geofire,我如何检查 Geofire 以注销用户?
问题描述
问题总结:我是Geofire的Android Studio和Firebase新手,请温柔。我的目标是当用户在我的应用程序的地图屏幕上没有 Geofire 位置时将其注销。我不知道如何在我的 Geofire 中查询“g”。如何查询“g”子项以查看它是否已创建?如果未创建“g”,则需要注销它们。所以目标是查询“g”并创建方法,如果“g”为空,则将用户注销。
我做过的事情:很多 Stackoverflow 帖子都没有使用 Android Studio。我确实发现我已经在 firebase 中使用 geofire 插入了位置,但它并没有解决我的问题。我也试过谷歌火力检查孩子是否存在
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,我希望用户退出,但现在用户没有退出。
解决方案
这是一个调用示例,getLocation
以查看供应商是否注册了位置。
来自 javadocs 的LocationCallback.onLocationResult
:
使用键的当前位置调用此方法。如果 GeoFire 中没有存储密钥的位置,则 location 将为空。
final String vendorKey = "someVendorKey";
geoFire.getLocation(vendorKey, new LocationCallback() {
@Override
public void onLocationResult(String key, GeoLocation location) {
if (key.compareTo(vendorKey) == 0) {
if (location == null) {
// vendor has not logged a location yet (or it was removed)
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
推荐阅读
- google-cloud-platform - 开始日期和结束日期字段以匹配数据工作室开始和结束期间
- c++ - 代码在我的电脑上运行良好,但我在提交到 coursera 时遇到了未知信号 11 问题
- sql - Oracle 在选择时为不同的情况返回相同的别名
- mysql - MYSQL中=和in的操作会导致不同的结果
- mysql - 驱动com.mysql.cj.jdbc.Driver声称不接受jdbcUrl
- sql - 更新查询不生效
- apache-spark - 使用 spark 对具有多列的大 DataFrame 进行排序,原因超过 spark.driver.maxResultSize
- c# - 如何通过c#向电报机器人添加两个内联按钮?
- xcode - XCode 在为 Mac Catalyst 构建后无法在 Assets.xcassets 中找到图像
- javascript - 在每个奇数/第二个循环上显示内部 HTML 输出的分数?