Learn Courses My Dashboard

Persistent JSON data

Im going through the 90 day course and have a question about using JSON data in the real world.

Is JSON data persistent? That is when a user makes a change and then closes the app, does it get saved anywhere? If not, what should I be using to save a user’s data every time they close the app?

No, it is not by default. You could write out the data after having made changes so the next time you opened the App, the data changes are reflected. Essentially you encode the data and then save it to a file. Next time you open the App, that file is then decoded into your data Model.

Yup what Chris said!

Here’s a tutorial for doing that, I’ll be using in one of my apps

https://www.hackingwithswift.com/books/ios-swiftui/writing-data-to-the-documents-directory

Thanks both of you, this is great! Im going to check it out now…
At what point would i want to start using core data or an online DB instead?

Are there some guidelines to help make the decision?

I would say once you start storing more than 50-100 items of data, you probably should begin looking into some database to use

Thanks @mikaelacaron
I just thought of this… so if i use JSON data in my app, and have a blank JSON ‘template’ file in mt app bundle… then a user starts using the app and it modifies their local JSON data… when I update my app, it would resend a blank ‘template’ JSON file and overwrite their data :frowning: What’s the best practice around this setup?

I don’t think you need to have a “blank template”

Whenever you go to write to the JSON file, the system checks if it exists, if it doesn’t, it’ll create the file and add to it.