Core Data: Predicate for first element in orderd r

2019-01-28 21:13发布

问题:

I have this to-many relationship which contains at least one element:

Appointment <<------>> Invitee

appointment.invitees is an ordered relationship resulting in an NSOrderedSet.

In a table view controlled by a fetched results controller, I have the appointments listed, along with the first element of the invitees set.

Now I want to search this list by invitees' names, using an NSPredicate. But how can refer to the first element of the ordered list in the predicate?

I tried:

fetchRequest.predicate = [NSPredicate predicateWithFormat:
       @"invitees[FIRST].lastName CONTAINS[cd] %@", searchTerm];

but I get the unimplemented SQL generation for predicate error. This would make sense, as the result is a collection, but not strictly an array. Still, I think that the sqlite store must be modeling the order, so it should be retrievable.

Any advice?

回答1:

Clearly, this a question that not even the most expert Core Data savants can answer.

The workaround is to model the first element as a separate to-one relationship and the rest as an optional to-many relationship based on the same related entity.

Appointment <<------>  mainInvitee        (Invitee)
            <<------>> additionalInvitees (Invitee)