Hi all...
The app I'm building is not really a beginner level test app, it's intended to be published so I want everything to be done properly while I'm both learning and building the app. I'm new to swift ecosystem but well experienced with python and JS ecosystems.
These two models are causing my app to crash
@Model final class CustomerModel { var id: String = UUID().uuidString var name: String = "" var email: String = "" var phone: String = "" var address: String = "" var city: String = "" var postalCode: String = "" var country: String = ""
@Relationship(deleteRule: .nullify)
var orders: [OrderModel]?
@Relationship(deleteRule: .nullify)
var invoices: [InvoiceModel]?
init() {}
}
@Model final class OrderModel { var id: String = UUID().uuidString var total: Double = 0 var status: String = "processing" var tracking_id: String = "" var order_date: Date = Date.now var updated: Date = Date.now var delivery_date: Date? var active: Bool = true var createdAt: Date = Date.now
var items: [OrderItem]?
@Relationship(deleteRule: .nullify)
var invoice: InvoiceModel?
@Relationship(deleteRule: .nullify)
var customer: CustomerModel?
init() {}
}
both referenced in this model:
@Model final class InvoiceModel{ var id: String = UUID().uuidString var status: String = "Pending" var comment: String = "" var dueDate: Date = Date.now var createdAt: Date = Date.now var updated: Date = Date.now var amount: Double = 0.0 var paymentTerms: String = "Once" var paymentMethod: String = "" var paymentDates: [Date] = [] var numOfPayments: Int = 1
@Relationship(deleteRule: .nullify, inverse: \OrderModel.invoice)
var order: OrderModel?
@Relationship(deleteRule: .nullify)
var customer: CustomerModel?
init() {}
}
This is my modelContainer in my index structure:
@main struct Aje: App { var appContainer: ModelContainer = { let schema = Schema([UserModel.self, TaskModel.self, SubtaskModel.self, InventoryModel.self, SupplierModel.self])
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false, allowsSave: true, groupContainer: .automatic, cloudKitDatabase: .automatic)
do{
return try ModelContainer(for: schema, configurations: [config])
}catch{
fatalError("An error has occured: \(error)")
}
}()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(appContainer)
}
}
This works fine but the below after adding the problematic models crashes the app unless CloudKit is disabled
@main struct Aje: App { var appContainer: ModelContainer = { let schema = Schema([UserModel.self, TaskModel.self, SubtaskModel.self, InventoryModel.self, SupplierModel.self, InvoiceModel.self, OrderModel.self, CustomerModel.self])
let config = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false, allowsSave: true, groupContainer: .automatic, cloudKitDatabase: .automatic)
do{
return try ModelContainer(for: schema, configurations: [config])
}catch{
fatalError("An error has occured: \(error)")
}
}()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(appContainer)
}
}