This is an additional scenario for the question and answer covered here.
Sample code is available here.
Original problem was related to this predicate:
let aPred = #Predicate<Event> {
$0.dateStart != nil &&
($0.dateStart >= startOfDay && $0.dateStart <= endOfDay) ||
($0.dateEnd >= startOfDay && $0.dateEnd <= endOfDay) ||
($0.dateStart < startOfDay && $0.dateEnd >= startOfDay)
}
This was giving compiling error:
The compiler is unable to type-check this expression in reasonable
time; try breaking up the expression into distinct sub-expressions.
This can be addressed with the help of this extension: (credit Mojtaba Hosseini)
extension Optional: Comparable where Wrapped: Comparable {
public static func < (lhs: Wrapped?, rhs: Wrapped?) -> Bool {
guard let lhs, let rhs else { return false }
return lhs < rhs
}
}
Now the problem is that if I add to my @Model final class TestEvent
one more optional bool var isActive: Bool?
I get the same error again.
let aPred = #Predicate<TestEvent> {
$0.isActive == true && //comment this out and it will compile
$0.dateStart != nil &&
($0.dateStart >= startOfDay && $0.dateStart <= endOfDay) ||
($0.dateEnd >= startOfDay && $0.dateEnd <= endOfDay) ||
($0.dateStart < startOfDay && $0.dateEnd >= startOfDay)
}
Conditions:
- SwiftData (not CoreData)
- CloudKit support (need to keep all properties as optional)
- Having a nil value is a valid scenario (cannot force unwrap)
Ideal solution will be a possibility to break the predicate down into smaller predicates and combine all them in a similar way as it is possible in CoreData using NSCompoundPredicate