Post

Replies

Boosts

Views

Activity

SwiftData - 3 Models referencing each other not possible?
Hi fellow Swifties, Been pulling my hair out for 8+ hours and need your help :( Tried creating 3 models that are cloudkit compatible, but the app crashes every time I try to add an Object to the modelContext. Error: Thread 1: EXC_BREAKPOINT (code=1, subcode=0x18ec49d2c) My guess: I think it is trying to force unwrap a nil... Can do: Insert Item() and Location() into modelContext + syncronizing to cloudkit without any errors. modelContext.insert(Item()) modelContext.insert(Location()) Can not do: Insert Object() into modelContext. modelContext.insert(Object()) Am I not initialising them correctly? Is this approach bad? Or am I experiencing a bug? Help is much appreciated <3 My 3 models: @Model class Item: Identifiable { var id: UUID = UUID.init() var name: String = String() var barcode: String = String() @Relationship(deleteRule: .cascade, inverse: \Object.item) var objects: [Object]? @Attribute(.externalStorage) var photo: Data? init(name: String = String(), barcode: String = String(), photo: Data? = nil) { self.name = name self.barcode = barcode self.photo = photo } } @Model class Location: Identifiable { var id: UUID = UUID.init() var name: String = String() @Relationship(deleteRule: .cascade, inverse: \Object.location) var objects: [Object]? var isFavorite: Bool = Bool(false) var unwrappedObjects: [Object] { objects ?? [] } init(name: String = String(), isFavorite: Bool = Bool(false)) { self.name = name self.isFavorite = isFavorite } } @Model class Object: Identifiable { var id: UUID = UUID.init() var amount: Int = Int() var expiryDate: Date? = nil var location: Location? var item: Item? init(amount: Int = Int(), expiryDate: Date? = nil) { self.amount = amount self.expiryDate = expiryDate } }
1
0
552
Jan ’24