UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Need Advice On Storing Data

Forums > SwiftUI

Hey All,

I'm working on a small personal project that, originally, was just going to store data to UserDefaults. All of the data is small (a handful of dates, some booleans, a few Int's, and a couple of strings). The rub is that in the development of the project, I have realized that I must persist the users birthdate in order to generate a constantly true result (previously I was using the users birthdate to create another data that was then persisted, but future calculations become impossible this way). So, I need to persist the users birthdate, and I don't want to do that in UserDefaults for security.

I have also decided to generate a printable document for the user that I will inevitably have to retrieve from the document directory. The user will never store more records than what the app asks for -- the amount of information needed is fixed. So for the data the user is submitting, would you store this in SwiftData or just use the document directory for the whole thing? My confliction is that each table would only have a single record, which seems sort of overkill compared to storing everything in a document and reading it from there when needed...

Any input would be appreciated. Thanks, Andy

2      

Use the Keychain to store any sensitive data.

If you can save your app's data in a single file, do that instead of using SwiftData or Core Data. For your situation save the data to a file (document) and retrieve it to create a printable document.

3      

would you store this [data, ints, birthday, blah, blah, blah] in SwiftData or just use the document directory for the whole thing?

I wouldn't use SwiftData unless you have a number of records with a similar structure. Your use cases point to a single object with many parameters. Yet for other reasons, it's not benficial to store this in UserDefaults.

One option for you to consider, save the user's data in a single JSON file. @twoStraws has a number of lessons on this excellent site where he reads, modifies, and stores an json file to a user's device. This solution seems well suited for your needs.

Sensitive Data

But what about the sensitive data?

First, I'd worry about getting the data into json and saving it, modifying it, then storing it back again to the user's drive.

After this is working great, you might consider looking into Apple's CryptoKit library.

See -> CryptoKit

But this might be overkill?

Take a look at this article on Apple's site on file encryption at rest. While a file is written to your device, it can be encrypted. It stays encrypted until you unlock your iPhone or iPad. Maybe this is what you really want?

See -> Encrypted at Rest

Keep Coding!

Please return here and let us know what path you selected. And share your advice and code!

2      

@Obelix, thank you for your help. I'm going to pursue this method and see what I can do!

EDIT: It looks like a lot of the XML tutorials are gone, and it's been mostly replaced by JSON. So I think I'm going to work on a JSON approach? Not the most elegant but since there's no function in which the user can create new records (only update existing ones) I don't think it will be a problem.

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.