Sorry, I wasn't able to test my answer earlier and was confident that this kind of query worked but I end up with the same error: to-many key not allowed here.
I tried other alternatives but couldn't find one that worked. Doesn't seem like it's possible to use optional to-many relationships inside a Predicate (even checking for $0.tags != nil or similar will crash)
A workaround would be to query on the to-one side of the relationship, i.e Tag and compute the array of Item from the results.
It's not ideal but I couldn't find another way. Since it's working for non-optional relationships we can only hope that this will possible in future releases.
struct ItemsView: View {
@Query private var tags: [Tag]
var items: [Item] {
self.tags.lazy.compactMap(\.items).flatMap { $0 }
}
init(searchText: String) {
self._tags = Query(filter: #Predicate<Tag> {
$0.name.localizedStandardContains(search)
})
}
}
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags: