I’ve been looking for a solution to this for a while. I finally found this method at the bottom of another thread:
let searchPredicate = #Predicate<SectionsSD> {
$0.toArticles?.contains(filter) ?? false
}
Which appears to be working for me as an empty/nil check here:
let descriptor = FetchDescriptor<Flashcard>(
predicate: #Predicate {
$0.annotations?.contains { _ in true } ?? false
}
)
I was able to combine it with an expression to actually look inside the to-many side:
let hasAnnotation = #Expression<Flashcard, Bool> { card in
if let annotations = card.annotations {
return annotations.contains { annotation in
annotation.reference == "hola"
}
} else {
return false
}
}
let descriptor = FetchDescriptor<Flashcard>(
predicate: #Predicate {
hasAnnotation.evaluate($0)
}
)
Seems to be working so far.
Topic:
App & System Services
SubTopic:
SwiftData
Tags: