Making Item optional fixes the problem: var item: Item?
Post
Replies
Boosts
Views
Activity
workaround
@Model
final class Item {
var name: String
var id: UUID
@Relationship(deleteRule:.cascade, inverse: \Entry.item) var entries: [Entry]
init(name: String) {
self.name = name
self.id = UUID()
entries = []
}
}
extension Item {
private var _entries: [Entry]? {
return try? self.context?.fetch(FetchDescriptor<Entry>(predicate:#Predicate<Entry>{
$0.item.id == id},sortBy: [SortDescriptor(\Entry.date, order: .forward)]))
}
}
extension Item {
func checkingDate()->Date?{
let _ = _entries
//...
return Date()
}
}
But is really awkward not allowed directly to access the collection property inside one entity...
yes, works
https://github.com/quaternionboy/LifeKPIs-SwiftData-Data-Model/tree/Profiling-LifeKPIs-Tests
https://github.com/quaternionboy/LifeKPIs-SwiftData-Data-Model
https://github.com/quaternionboy/LifeKPIs-SwiftData-Data-Model
in MainActor