0

I have something like this:

struct Main: View { @ObservedObject var data: Data = Data() @ObservedObject var profiles: Profiles = Profiles() var body: some View { TabView{ Discover().tabItem { Image(systemName: "book") Text("Discover") } // more tabs } } } 

My issue is that this data and profiles are bound to a database and initiated when the app starts. When the app gets started and no user is logged in there will be no data accessed. As the data is user specific content on the Database (Firestore).

Now when logging in the data doesn't appear until a restart of the app as the vars have already been initiated with empty database content. As content is bound to the user.

Id need to reinitiate them or "refetch" them once the users logs in or a user is logged in. Doing this with onAppear is not optimal as this would drive the database traffic up.

I use Firestore as my database.

5
  • When or where do you want to reinitialise them? Commented May 27, 2020 at 17:09
  • 1
    Firebase Authentication has an auth state listener which you can add anywhere in the app to handle sign-in and sign-out events Auth.auth().addStateDidChangeListener { (auth, user) } Commented May 27, 2020 at 18:25
  • The question is a bit unclear and we don't know what bound to a database means specifically. Usually when and app starts it asks the user to log in (authenticate) and when that's successful, you would populate the variables. What's preventing you from doing that? Are you having an issue with some code? Can you clarify and update the question? Commented May 27, 2020 at 18:40
  • Id like to reinitialise them after a user logged in as when there is no user logged in it is empty and stays empty as it doenst “refetch” upon login. Im using Firebase for the user logins. Commented May 27, 2020 at 21:08
  • If we think through the process, if a user is not logged in those should be empty, right? There's not data to display! They should only be populated after a user authenticates so you know what data to get. The problem with the question is the code has nothing to do with the actual question - you're asking about the authentication process so we would need to see that code and how it interacts with the current code in the question to suggest a fix. So, please update your question with a minimal amount of code to reproduce the issue. Commented May 29, 2020 at 15:19

1 Answer 1

1

what about something like

struct Main: View { @ObservedObject var data: Data = Data() @ObservedObject var profiles: Profiles = Profiles() var body: some View { TabView{ Discover().tabItem { Image(systemName: "book") Text("Discover") } // more tabs }.onAppear() { if !loggedIn() { <<< func which checks whether user is logged in or data is nil loginAndFillData() <<<<< login } } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Does this work? Isn’t the Tabbar always present? Also this is not optimal as the data would get fetched on every appear when there is a user logged in. This would drive my database traffic high

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.