Sorry for the delay in getting back to you; trying to actually have a
weekend. :-)

First: I'm assuming that your model looks something like this, with two
entities, and reciprocal to-many relationships between then (whether you're
using exactly the same names or not): http://tinypic.com/r/11v2m8n/8

If we weren't talking about a fetched results controller, getting all books
for a library would be quite simple, of course: just follow the
relationship.

    // without dedicated subclasses
    NSManagedObject *library; // assume this gets initialized somewhere
    NSSet *booksDirect = [library valueForKey:@"books"];

    // with dedicated subclasses
    FooLibrary *library;
    NSSet *booksDirect = [library books];

With the fetched results controller, you do need to use a fetch request to
get the books, but the relationship is still the way to go:

    // get the library's books through a fetch request
    NSFetchRequest *fetchBooks = [NSFetchRequest
fetchRequestWithEntityName:@"Book"];
    [fetchBooks setPredicate:[NSPredicate predicateWithFormat:@"ANY
libraries == %@", library]];

In this case, because the Book -> Library relationship is to-many, the
predicate needs to check whether ANY of the book's libraries match the
particular library you care about. (The value inserted into the predicate
can be either the library, or the library managed object ID, which can be
handy in the case where don't have the entire library object loaded.)

Sixten



On Sat, Mar 1, 2014 at 8:33 AM, Koen van der Drift <
koenvanderdr...@gmail.com> wrote:

> You are right about the [cd], which is not supposed to be here, blame it
> on poor copy-paste skills.
>
> So how can I fix the predicate to compare entities?
>
> - Koen.
>
> On Mar 1, 2014, at 10:22 AM, Sixten Otto <hims...@sfko.com> wrote:
>
> Two things:
>
> 1. No, doing a string comparison with contains (and case and diacritical
> folding active) is one of the slower kinds of string comparison. Straight
> equality should be much faster.
>
> 2. You say that there are two entities, with a relationship defined. Why,
> then, are you doing a string comparison in this predicate? Checking the
> value of the relationship should be much faster.
>
> Sixten
>
>
> On Sat, Mar 1, 2014 at 5:22 AM, Koen van der Drift <
> koenvanderdr...@gmail.com> wrote:
>
>> Consider two entities in a many-to-many relationship: Library and Book.
>> In a NSFetchResultController backed UITableView, I like to show all the
>> books from one library, so I constructed the following fetch request:
>>
>>     NSFetchRequest *fetchRequest = [NSFetchRequest
>> fetchRequestWithEntityName: @"Book"];
>>     NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:
>> @"date" ascending: YES];
>>     [fetchRequest setSortDescriptors: @[sort]];
>>
>>     NSPredicate *predicate = [NSPredicate predicateWithFormat:
>> @"libraries contains[cd] %@", self.library];
>>     [fetchRequest setPredicate: predicate];
>>
>> which is then fed into the NSFetchedResultController when the view opens.
>>
>> There is a noticeable delay of a few 100 ms when the view opens, and I
>> suspect that the predicate I have is not optimal by using 'contains'.
>>
>> Is there a way to improve on this?
>>
>> Thanks,
>>
>> - Koen.
>>
>>
>> _______________________________________________
>>
>> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>>
>> Please do not post admin requests or moderator comments to the list.
>> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>>
>> Help/Unsubscribe/Update your Subscription:
>> https://lists.apple.com/mailman/options/cocoa-dev/himself%40sfko.com
>>
>> This email sent to hims...@sfko.com
>
>
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to