@Model final class Entry {
var value: Int
var date: Date
var item: Item?
init(value: Int, date: Date) {
self.value = value
self.date = date
}
}
@Model
final class Item {
var name: String
@Relationship(deleteRule:.cascade, inverse: \Entry.item) var entries: [Entry]
init(name: String) {
self.name = name
entries = []
}
}
extension Item {
func checkingDate(in context: ModelContext)->Date{
try! context.fetch(FetchDescriptor<Entry>(predicate:#Predicate<Entry>{$0.item == self})).last!.date
}
}
same if self == self
Why I'm fetching and not directly accessing entries: https://developer.apple.com/forums/thread/735735