2017-06-21 30 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