Post

Replies

Boosts

Views

Activity

Reply to SwiftData Predicate Issue
I actually have found a workaround. Here it is. let predicate = #Predicate<Data> { data in data.result == result && data.variable?.starts(with: SomeString) ?? false } Note the data.variable?. and the ?? false. This works. I still don't think I should have to discover these things. Something needs to be fixed on Apple's end.
Aug ’23
Reply to SwiftData and 'Circular references'
Actually, I think it can be as simple as the following example @Model final public class MainItem { var title: String? @Relationship(deleteRule: .cascade) var childs: [ChildItem]? init(title: String? = nil, childs: [ChildItem]? = []) { self.title = title self.childs = childs } } And the child item is. @Model final public class ChildItem { var timestamp: Date? // Add inverse as a simple variable var item: MainItem? init(timestamp: Date? = nil, item: MainItem? = nil) { self.timestamp = timestamp self.item = item } } In the init of the MainItem, set the childs to 'childs: [ChildItem]? = [].' Do NOT set it to nil or you'll crash. I did.
Aug ’23
Reply to SwiftData and 'Circular references'
Well, my code is crashing with the following error. SwiftData/BackingData.swift:367: Fatal error: Unknown related type - ChildItem My MainItem @Relationship @Relationship(deleteRule: .cascade, originalName: "events", inverse: \ChildItem.item) var items: [ChildItem]? My ChildItem var item: MainItem? It also crashes when using @Relationship var item: MainItem? for the ChildItem.
Aug ’23