ios – SwiftUI: The issue is saving knowledge in CoreData


I wish to save the info within the database profileDB.lvl that’s transferred from one other property self.lvl

In the beginning of ContentView I prescribed:

@Setting(.managedObjectContext) var managedObjectContext

And

@State public var lvl = 0
let profileDB = ProfileUser(context: self.managedObjectContext)

Then, within the button beneath, I prescribed the logic of saving:

Button(motion: {
    withAnimation(.easeInOut){

        profileDB.lvl = Int16(self.lvl)

        do{
            if progress < 300{
                progress += 75
            } else if progress == 300{
                progress = 50
                self.lvl += 1
            }
            
            strive self.managedObjectContext.save()
        } catch{
            print(error)
        }
    }
},
       label: {
    Textual content("Carry out")
        .foregroundColor(Coloration.black)
        .font(.system(dimension: 18, design: .default))
})

And it seems that the info is modified in self.lvl, however not assigned to profileDB.lvl
Am I having bother with the right implementation, what might help resolve the issue?

Leave a Reply