I'm trying to understand and learn SwiftData. I want to keep some data in my application. I wanted to use swiftData to learn SwiftData and created a model like this:
import Foundation
import SwiftData
@Model
final class SwiftDataModel {
var height: Optional<Double>
var heightUnit: Optional<String>
var weight: Optional<Double>
var weightUnit: Optional<String>
var gender: Optional<String>
var waterGoal: Optional<Double>
var volumeUnit: Optional<String>
var activity: Optional<Int>
var age : Optional<Int>
var nutritionLog : Optional<Array<Any>>
init(height: Optional<Double>, heightUnit: Optional<String>, weight: Optional<Double>, weightUnit: Optional<String>, gender: Optional<String>, waterGoal: Optional<Double>, volumeUnit: Optional<String>, activity: Optional<Int>, age: Optional<Int>, nutritionLog: Optional<Array<Any>>) {
self.height = height
self.heightUnit = heightUnit
self.weight = weight
self.weightUnit = weightUnit
self.gender = gender
self.waterGoal = waterGoal
self.volumeUnit = volumeUnit
self.activity = activity
self.age = age
self.nutritionLog = nutritionLog
}
}
Maybe I can also add an array like weightlog. As far as I understand swiftData keeps this data in a list. Therefore, I will always need to update index 0 instead of using context.insert(data). Is this true?
My other case is this: After the user logs in, can I check whether the user's age, weight and height information is kept in SwiftData and determine the page to be opened there?
For example :
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
if (ViewModel.isLogin() && !ViewModel.userId.isEmpty ) {
// If height, weight, and age information is available in swiftData TabBarView()
// else IntroPageView()
{
} else {
LoginView()
}
}
}
}
Is such a check possible or should I use "User Deafults" or "Core Data" instead of SwiftData in my application?
I'm looking forward to your answers. Thanks in advance
Welcome to Stack Overflow, I can see you are on your way to using SwiftData - and I will try answer your questions as best I can
I am not too sure what you mean by this. I am guessing you are asking how to update the model and add a new attribute. What you'll need to do is add a
weightlogin the code to add it in. Ensure it is a optional like the rest and have a default value asnullif you already have data in your application.However, from your usage in your app, it seems like you would be better off using User Defaults - this is because you will only have one weight, height e.c.t for each user. SwiftData and CoreData could be used in this scenario if you have multiple users on the same phone - which is rare for any health application.
But, with any form of persistent data, you have to retrieve the data - then save it to a
@Statevariable. If that@Statevariable changes in your view (a user types in a new height or weight), you'll need to save that value to the User Defaults again.Once you have saved that data to the User Defaults. If the user closes the app and restarts, you can implement something like this where on app launch the
initfunction is run and it checks if the height, weight, and age is present in the User Defaults and presents the correct view.Then to save to User Defaults use: