The topic line says all of it.
After I run the code from Xcode, file is saved however with no values once I once more run the app from Xcode to examine if data are saved appropriately. Nonetheless, once I run the app immediately from simulator, it simply works nice. And whereas operating from simulator, I’m killing the app from background to make certain it relaunches from begin. It simply works nice.
SwiftData code I’m utilizing :
App :
import SwiftUI
import SwiftData
@essential
struct PennywiseApp: App {
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Transaction.self,
Category.self
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return strive ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Couldn't create ModelContainer: (error)")
}
}()
var physique: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(sharedModelContainer)
}
}
View declaration :
struct Classes: View {
@State var selectedCategory: Int = 0
@State var showNewCategory: Bool = false
@Setting(.modelContext) var modelContext
@State non-public var path = [Category]()
@Question var classes: [Category]
NavigationStack and Record :
NavigationStack(path: $path) {
Record {
Part {
ForEach(filteredCategories) { class in
NavigationLink(worth: class) {
Circle()
.stroke(Coloration(hex: class.hex) ?? Coloration.blue, lineWidth: 3)
.fill(Coloration(hex: class.hex) ?? Coloration.blue)
.body(width: 25, top: 25)
Textual content("(class.identify)")
}
}
.onDelete(carry out: deleteCategory)
}
}
.navigationTitle("Classes")
.navigationDestination(for: Class.self) { class in
NewCategory(class: class)
}
Add and delete features :
func addCategory() {
var class: Class
if selectedCategory == 0 {
class = Class(identify: "", hex: "", transactionType: TransactionType.Earnings)
} else {
class = Class(identify: "", hex: "", transactionType: TransactionType.Expense)
}
modelContext.insert(class)
strive! modelContext.save()
path.append(class)
}
func deleteCategory(at offsets: IndexSet) {
for offset in offsets {
modelContext.delete(filteredCategories[offset])
}
strive! modelContext.save()
}
Second view :
struct NewCategory: View {
@State var selectedFlow: Int = 0
@Bindable var class: Class
var pickerColor: Binding { // << right here !!
Binding {
return Coloration(hex: class.hex) ?? Coloration.blue
} set: { newRGBAColor in
class.hex = newRGBAColor.toHex() ?? "ffffff"
}
}
var physique: some View {
Type {
Part {
Picker(choice: $class.transactionType, label: Textual content("Class Sort")) {
ForEach(TransactionType.allCases) { kind in
Textual content(kind.rawValue)
}
}
}
Part {
TextField("Class Identify", textual content: $class.identify)
.textContentType(.identify)
}
Part {
HStack {
Textual content("Choose Class Coloration")
.foregroundStyle(.black)
.body(maxWidth: 200)
.padding(.main, -11)
Spacer()
// bgColor = Coloration(hex: bgColor.toHex() ?? "") ?? Coloration.blue
ColorPicker("Choose Class colour", choice: pickerColor)
.padding(.trailing, 20)
.labelsHidden()
}
}
}
.navigationTitle("Add/Edit Class")
.navigationBarTitleDisplayMode(.inline)
}
}