I have not been capable of finding an answer to a predicate that filters on an optionally available one to many relationship. The connection must be optionally available in an effort to work with CloudKit. I’ve recreated this with a quite simple venture. Let’s assume I am searching for a dad and mom with a child by the title of ‘Abbijean’.
Mannequin.swift
@Mannequin
class Mum or dad {
let identifier: UUID
let title: String
@Relationship(deleteRule: .cascade, inverse: Child.guardian) var youngsters: [Kid]?
init( title: String, youngsters: [Kid]? = nil) {
self.identifier = UUID()
self.title = title
}
}
@Mannequin
class Child {
let identifier: UUID
let title: String
var guardian: Mum or dad?
init(title: String, guardian: Mum or dad? = nil) {
self.identifier = UUID()
self.title = title
}
}
ContentView.swift
struct ContentView: View {
@Setting(.modelContext) var modelContext
@Question(filter: #Predicate<Mum or dad> { guardian in
// guardian.youngsters?.accommodates(the place: {
// $0.title == "Abbiejean"
// }) != nil
guardian.youngsters.flatMap { youngsters in
youngsters.accommodates(the place: { $0.title == "Abbijean" })
} == true
}) var dad and mom: [Parent]
var physique: some View {
Button(motion: {
var parent1 = Mum or dad(title: "Sterling Archer")
parent1.youngsters?.append(Child(title: "Abbiejean"))
modelContext.insert(parent1)
}, label: {
Textual content("Add Information")
})
Record {
ForEach(dad and mom, id: .id) { guardian in
Textual content("(guardian.title)")
}
}
}
}
When utilizing:
guardian.youngsters?.accommodates(the place: {
$0.title == "Abbiejean"
}) != nil
The compiler tells me to make use of a flatMap when unwrapping optionals
Utilizing a flat map:
guardian.youngsters.flatMap { youngsters in
youngsters.accommodates(the place: { $0.title == "Abbijean" })
} == true
The app crashes with: Thread 1: “to-many key not allowed right here”
Examined in Xcode 15.0.1 (iOS 17.0.1) and 15 Beta 3 (iOS 17.2)