0

I'm having trouble figuring out how to save data in my SwiftUI @EnvironmentObject instance. I created an instance of my @EnvironmentObject type, UserData, in my SceneDelegate file, which then gets passed into my root view which is then accessed and modified by my other views.

My question is, how would one save the data in such an instance for access after the user closes and re-opens the app?

For example, I have a hasDoneSetup property in my UserData instance which defaults to false, so that my first-time setup sequence runs only the first time the app is opened. The setup sequence disappears when the setup is completed, which is correct and working fine. However, when I close and re-open the app, it seems that a new UserData instance is being made, as the setup sequence runs again.

Here's some of my SceneDelegate code:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    // Other properties omitted

    var userData = UserData()

    // Other code omitted

    // Use a UIHostingController as window root view controller
    if let windowScene = scene as? UIWindowScene {
        let window = UIWindow(windowScene: windowScene)

        // This line is where I pass the UserData instance to my root view.
        // I don't want to pass a new instance each time the app is opened however.
        // Rather, I want to make a new instance only the first time the app is opened, and use that instance anytime the app is opened henceforth.

        window.rootViewController = UIHostingController(rootView: MainScreen().environmentObject(userData))

        self.window = window
        window.makeKeyAndVisible()

    // Other code omitted
}

I would greatly appreciate any and all help! Thank you so much in advance and I hope you have a great day! :)

  • 1
    This isn't really a problem specific to SwiftUI; You can persist your `UserData` instance any way you like - You could use Core Data, you could serialise it to a JSON string and store it in a text file, you could use `UserDefaults` (Don't use `UserDefaults`) - Then when your app starts, you retrieve the persisted data if you can, create an empty instance if you can't - – Paulw11 Jul 09 '19 at 03:53
  • Upvoted @Paulw11 comment after deleting my answer. I think you need to think about this - how would you do this in iOS 12? in AppDelegate? It really is how you should do it in iOS13.... except you need to account for scenes (and `SceneDelegate` instead of an app. If we aren't understanding your issue, please, update with more specifics. –  Jul 09 '19 at 04:32
  • Possible duplicate of [How do I use UserDefaults with SwiftUI?](https://stackoverflow.com/questions/56822195/how-do-i-use-userdefaults-with-swiftui) – Bogdan Farca Jul 09 '19 at 07:13
  • I want to thank you for your help. As of now, I have code written to save and load my UserData instance! I'm currently trying to figure out how to run the line of code required to save my instance to a file when the app is quit. If you know how to do this, let me know! – Ethan Marshall Jul 10 '19 at 18:03

0 Answers0