Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
782 views
in Technique[技术] by (71.8m points)

ios - Core Data: Predicate for first element in orderd relationship

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...