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

Architecture question?

Forums > Swift

Thanks to all in advance for this great forum!

I am fairly new to Swift & Native iOS

I am building an app in SwiftUI following MVVM I am struggling a bit on the best way to save and persist data for my app.

The user flow is user fills out a form consisting of 2 strings, a price and either selects a default local image or uses their own image. Then an instance of a struct is created that user can tally a quantity and price subtotal for that instance.

The data consists of a couple of strings and a number, also user selects from a selection of default images which would be basically a string or take an image of their own witch I assume is just a reference to an image on users device. I think there would be less than 20 instances of each struct on app.

My question is what is the best way to save and persist this data? Is core data really necessary?

Thanks so much for feedback it really means a lot to me

Cheers Mike

3      

I'm not sure I understand the user flow. Or what the app is doing. So I will go with what I think you mean:

1- User fills out a form that has 2 input strings, one input number and one image. 2- Upon selection, the user will then be able to change the quantity which updates the subtotal so far.

This doesn't provide enough info about your Model. But my guess is the entire model is:

  • string1
  • string2
  • price
  • image
  • quantity
  • total

Anyway, all that to simply say that it seems Core Data can help for sure. But your question "is it really necessary?" reminds me of when I first started with Core Data. Let me tell you it's not that difficult to implement and it does not come with some performance overhead. Especially nowasdays, it can be as little as 40 lines of code and you're up and running with it.

If you need to persist the data long term, then yes, Core Data is great, as it comes with many great features. You could of course, save your data as JSON in some file on disk. Or, since you say there will be no more than 20 entities of your model at any time, you might want to go with UserDefaults (or in SwiftUI @AppStorage).

I'm also not sure what MVVM has to do with it...

3      

@MarcusKay Thanks for the reply it means a lot to me.

The model is basically as you mentioned. So far from other feedback I have gotten I am leaning towards saving as JSON in the application support directory, just wanted to get other peoples feedback.

3      

This also depends if you plan to store other data later on. With JSON you need to be careful when changing the data model (if you decide to add another property or modify type). It is easy to forget this and you can end up with users who lose their data.

As for the saving the selected image, you need to copy it into your app directory. You cannot reference the Photos library for example.

3      

@nemecek-filip Thanks for the insight about saving image and storing other data later.

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.