How does Firebase sync work, with shared data?

后端 未结 3 1219
时光取名叫无心
时光取名叫无心 2020-12-11 07:44

I use Firebase to handle the Auth topic of my Android app. I also save a user profile on Firebase, that contain user id and extra options that user can update in the android

3条回答
  •  Happy的楠姐
    2020-12-11 08:36

    The problem comes from the fact that you're using disk persistence with a single-value event listener. When you do:

    ref.addListenerForSingleValueEvent(new ValueEventListener() {...
    

    You're asking for a single value of that location (the user in your case). If Firebase has a value for that location in its local cache, it will fire for that value straight away.

    The best way to solve this is to not use a single-value listener, but instead use a regular event listener. That way you will get two events: one for the cached version and one for the version that comes back from the server (if it is different).

    The only alternative is to not use Firebase's disk persistence. Without that, there won't be a local cache for the data to be read from upon a restart.

    There were a few discussions about this combination on the Firebase mailing list. Here's one: https://groups.google.com/forum/#!msg/firebase-talk/ptTtEyBDKls/XbNKD_K8CQAJ

提交回复
热议问题