As the title says I'm trying to use a to-one relationship as a sectionIdentifier in a @SectionedFetchRequest. The compiler is happy but there's a runtime crash:
Could not cast value of type '_NSCoreDataTaggedObjectID' (0x146c0f750) to 'MyApp.ServiceCategory' (0x104c4b3a0).
The fetch request:
@SectionedFetchRequest(
sectionIdentifier: \Service.serviceCategory,
sortDescriptors: [
SortDescriptor(\Service.active, order: .reverse),
SortDescriptor(\Service.displayText)
],
predicate: NSPredicate(format: "%K = %d", #keyPath(Service.active), true),
animation: .default
) var sectionedServices: SectionedFetchResults<ServiceCategory?, Service>
... and the breaking runtime code:
ForEach(sectionedServices /* here */) { section in
Section(header: Text(section.id?.displayText ?? "")) {
ForEach(section) { svc in
Text(svc.displayText ?? "")
}
}
}
The request works if I switch out the sectionIdentifier for the active property (which is a Bool property rather than a relationship). It also works if I switch it out for displayText which is an optional String, so it seems to be a problem trying to section by a relationship rather than with it being an optional.
The error suggests the request is returning a Core Data fault rather than an object but my attempts to somehow unwrap this haven't gone very far.
Any thoughts would be greatly appreciated!
4
0
2.3k