簡體   English   中英

如果未創建Geofire,如何檢查Geofire以注銷用戶?

[英]How can I check Geofire to log user out if no Geofire was created?

問題摘要:我是使用Geofire接觸Android Studio和Firebase的新手,請保持謙虛。 我的目標是在用戶不在我的應用的地圖屏幕上但沒有Geofire位置的情況下注銷該用戶。 我無法弄清楚如何在我的Geofire中查詢“ g”。 如何查詢“ g”子項以查看它是否已創建? 如果未創建“ g”,則需要注銷它們。 因此,目標是查詢“ g”並創建方法,如果“ g”為空,則將用戶注銷。

我所做的事情:許多Stackoverflow帖子都沒有使用Android Studio。 我確實找到了這個, 我已經在firebase中使用geofire插入了位置,但是並不能解決我的問題。 我還嘗試了Google Firebase檢查孩子是否存在

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

這是調用getLocation的示例,以查看供應商是否注冊了位置。

來自LocationCallback.onLocationResult javadocs:

使用密鑰的當前位置調用此方法。 如果密鑰的GeoFire中沒有存儲任何位置,則location將為null。

        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) {

            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM