2017-06-21 50 views
0

在我的應用程序中,我使用Firebase存儲數據。每次用戶的位置發生變化時,都會調用一個函數將新座標寫入Firebase。但是,我正在使用多個帳戶,並且該程序將信息寫入錯誤的子節點。我似乎無法找到問題。我的代碼應該使情況更加清晰:Firebase更新多個孩子錯誤

public void locationPusher(Location currentLocation) { 
    if (currentLocation != null) { 
     double lat = currentLocation.getLatitude(); 
     double lon = currentLocation.getLongitude(); 

     Log.e("Lat: ", "" + lat); 
     Log.e("Long: ", "" + lon); 

     Double[] data = new Double[]{lat, lon}; 

     FirebaseHelper firebaseHelper = new FirebaseHelper(); 
     firebaseHelper.pushToFirebaseOnLocationUpdate(firebaseUser, data); 
    } 
} 

上面的代碼是位置推,即每次調用的位置變化。最後,pushToFirebaseonLocationUpdate推動這個信息傳遞給正確的用戶節點,其中firebaseUser是登錄用戶當前:

public void pushToFirebaseOnLocationUpdate(@NonNull FirebaseUser loggedInUser, Double[] data) { 
    userRef = userDataRef.child(loggedInUser.getUid()); 

    userRef.child("location").child("latitude").setValue(data[0]); 
    userRef.child("location").child("longitude").setValue(data[1]); 
} 

不知何故,但是,當我註銷用戶,我用另一個賬號登錄,應用程序設置當前用戶和已經註銷的用戶中的位置節點。它爲什麼這樣表現?

+1

該代碼將數據寫入'userDataRef'下的特定用戶位置。沒有看到你如何初始化'firebaseUser'和'userDataRef',很難說更多的是你可能爲其中的一個緩存了陳舊的值。 –

+0

'userDataRef'是在調用靜態方法FirebaseHelper時定義的(只是在代碼中改變了它)。 'firebaseUser'初始化爲'FirebaseAuth.getInstance()。getCurrentUser()',它返回正確的用戶。 – Blank

+1

請在[單個,最小,完整,可驗證的示例](http://stackoverflow.com/help/mcve)中找出問題,然後編輯您的問題以包含此MCVE。有可能你會在建立這樣一個MCVE的時候發現你在哪裏緩存錯誤的用戶。 MCVE的一個步驟是使用'userRef = userDataRef.child(FirebaseAuth.getInstance()。getCurrentUser()。getUid());'而不是變量,看看是否有所作爲。 –

回答

0

確保在註銷時清除userRef變量。

+0

我試過了,但不幸的是,這並沒有幫助。 – Blank