Reputation: 1845
I'm building an app using Firebase Database, Auth & Storage.
Basically, my users can sign up / sign in via Firebase Auth, then I store data for each of these users on Firebase Database.
I have been looking for the best way to get the Current User's data available throughout the app without having to re-fetch the information I need from Firebase on different screens.
Very few resources on Stack. Some people do talk about creating a CurrentUser
singleton available globally, but others say that it's bad practice.
Now, maybe fetching the data as and when I need them might be the best practice solution, but I just felt it was strange to re-write the same piece of code again and again on each screens.
Any thoughts on this? I'd be really grateful if you guys have insights.
Upvotes: 2
Views: 1765
Reputation: 9945
You can make a struct with which you can access the values:-
struct userGlobalInfo{
var userID : String!
var userName : String!
var userEmail : String!
init(id : String!, name : String!, email : String!){
self.userID = id
self.userName = name
self.userEmail = email
}
}
Upvotes: 2
Reputation: 5659
Isn't the logged in user data already available globally? I mean you can always access any logged in user related data by -
let userID = FIRAuth.auth()!.currentUser!.uid
The way uid available in this code snipped you can access any data related to user as long as the user is logged in. Do check if the current user is not nil before executing that, but i assume you will anyways take care of that.
Upvotes: 5