Gaurav Takte
Gaurav Takte

Reputation: 615

Firebase user data storage issue

I am working with Firebase Authentication and while coding I realised that I can use User user = FirebaseAuth.getInstance().getCurrentUser; to fetch user data in any activity through out the application if user successfully logged in but I am confused that whether I can store user data in sharedPreferences after user login or I can use User user = FirebaseAuth.getInstance().getCurrentUser; way to get user data in whichever activity I want to. So which method is preferable to use firebase user data. And if there any other method you have to suggest then you are always welcome. Thanks in advance.

Upvotes: 0

Views: 91

Answers (3)

J.D
J.D

Reputation: 58

Use always firebase user instance, because it gives to you an updated user instance. if you are going to save user data in sharedPreferences, you should always save user details in sharedPreferences when you update the user data.

Upvotes: 1

ShineMan
ShineMan

Reputation: 354

The user data is limit to login info like user id or email address by using FirebaseAuth.getInstance().getCurrentUser(). If it's enough to use it for you, that's ok.

In order to get the data which saved to sharedpreferences, you can define PreferencesUtils class. The class is singleton or has static members. If so, I think you can call this members anywhere.

I hope it helps.. If you any questions, let me know.

Upvotes: 1

Doug Stevenson
Doug Stevenson

Reputation: 317427

I would use an AuthStateListener to listen to possible changes to the authenticated user, and persist changes to that user object only when they might change, according to that listener. It's probably not worthwhile to continually ping currentUser for changes when you know it can only change when response to the listener callback.

Upvotes: 0

Related Questions