Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While @BarryWark's answer is correct when working with fetch requests, I want to write a warning to the folks who will try to apply this rule to a filtering of Core Data to-many relationships. </p> <p>Shortly: If when filtering to-many relationships you use a predicate and your array of objects for IN query is an array of objectIDs - then you should use <strong>self.objectID</strong> in your query string like</p> <pre><code>NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(self.objectID IN %@)", arrayOfObjectIDs]; </code></pre> <p>Because using just <code>(self IN %@)</code> in the case of filtering to-many relationships will result in incorrect results - it is just an NSArray that evaluates predicates and it knows nothing about Core Data's NSManagedObjectID stuff.</p> <p>I've crafted special test code showing this. Sorry for so many lines, but they worth it. There are two entities: User and Post and User has a to-many relationship named "posts".</p> <pre><code>User *user = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([User class]) inManagedObjectContext:managedObjectContext()]; Post *post = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Post class]) inManagedObjectContext:managedObjectContext()]; [user addPostsObject:post]; [managedObjectContext() save:nil]; // 1. Both filtered relationship array and fetch result are correct! NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(self IN %@)", @[ post ]]; NSSet *filteredRelationship = [user.posts filteredSetUsingPredicate:predicate]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Post"]; NSArray *fetchResult = [managedObjectContext() executeFetchRequest:fetchRequest error:nil]; NSLog(@"\n\n\nPredicate: %@", predicate); NSLog(@"filteredRelationship: %@", filteredRelationship); NSLog(@"fetchResult: %@", fetchResult); // 2. Filtered relationship array is empty (wrong), fetch result is correct, ! predicate = [NSPredicate predicateWithFormat:@"(self IN %@)", @[ post.objectID ]]; filteredRelationship = [user.posts filteredSetUsingPredicate:predicate]; fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Post"]; fetchResult = [managedObjectContext() executeFetchRequest:fetchRequest error:nil]; NSLog(@"\n\n\nPredicate: %@", predicate); NSLog(@"filteredRelationship: %@", filteredRelationship); NSLog(@"fetchResult: %@", fetchResult); // 3. Filtered relationship array is empty (wrong), fetch result is correct predicate = [NSPredicate predicateWithFormat:@"(self.objectID IN %@)", @[ post ]]; filteredRelationship = [user.posts filteredSetUsingPredicate:predicate]; fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Post"]; fetchResult = [managedObjectContext() executeFetchRequest:fetchRequest error:nil]; NSLog(@"\n\n\nPredicate: %@", predicate); NSLog(@"filteredRelationship: %@", filteredRelationship); NSLog(@"fetchResult: %@", fetchResult); // 4. Filtered relationship array is correct, fetch result is correct predicate = [NSPredicate predicateWithFormat:@"(self.objectID IN %@)", @[ post.objectID ]]; filteredRelationship = [user.posts filteredSetUsingPredicate:predicate]; fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Post"]; fetchResult = [managedObjectContext() executeFetchRequest:fetchRequest error:nil]; NSLog(@"\n\n\nPredicate: %@", predicate); NSLog(@"filteredRelationship: %@", filteredRelationship); NSLog(@"fetchResult: %@", fetchResult); </code></pre> <hr> <p><strong>TLDR output</strong></p> <pre><code>&lt;redacted&gt; Predicate: SELF IN {&lt;Post: 0x2a04f10&gt; (entity: Post; id: 0x2a56c40 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/Post/p1&gt; ; data: { content = nil; title = nil; user = "0x2af2a20 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/User/p1&gt;"; })} &lt;redacted&gt; filteredRelationship: {(&lt;Post: 0x2a04f10&gt; (entity: Post; id: 0x2a56c40 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/Post/p1&gt; ; data: { content = nil; title = nil; user = "0x2af2a20 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/User/p1&gt;"; }) )} &lt;redacted&gt; fetchResult: ("&lt;Post: 0x2a04f10&gt; (entity: Post; id: 0x2a56c40 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/Post/p1&gt; ; data: {\n content = nil;\n title = nil;\n user = \"0x2af2a20 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/User/p1&gt;\";\n})") &lt;redacted&gt; Predicate: SELF IN {0x2a56c40 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/Post/p1&gt;} &lt;redacted&gt; filteredRelationship: {()} &lt;redacted&gt; fetchResult: ("&lt;Post: 0x2a04f10&gt; (entity: Post; id: 0x2a56c40 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/Post/p1&gt; ; data: {\n content = nil;\n title = nil;\n user = \"0x2af2a20 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/User/p1&gt;\";\n})") &lt;redacted&gt; Predicate: objectID IN {&lt;Post: 0x2a04f10&gt; (entity: Post; id: 0x2a56c40 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/Post/p1&gt; ; data: { content = nil; title = nil; user = "0x2af2a20 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/User/p1&gt;";})} &lt;redacted&gt; filteredRelationship: {()} &lt;redacted&gt; fetchResult: ("&lt;Post: 0x2a04f10&gt; (entity: Post; id: 0x2a56c40 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/Post/p1&gt; ; data: {\n content = nil;\n title = nil;\n user = \"0x2af2a20 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/User/p1&gt;\";\n})") &lt;redacted&gt; Predicate: objectID IN {0x2a56c40 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/Post/p1&gt;} &lt;redacted&gt; filteredRelationship: {(&lt;Post: 0x2a04f10&gt; (entity: Post; id: 0x2a56c40 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/Post/p1&gt; ; data: { content = nil; title = nil; user = "0x2af2a20 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/User/p1&gt;";}))} &lt;redacted&gt; fetchResult: ("&lt;Post: 0x2a04f10&gt; (entity: Post; id: 0x2a56c40 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/Post/p1&gt; ; data: {\n content = nil;\n title = nil;\n user = \"0x2af2a20 &lt;x-coredata://9D07BF41-2DC0-42C1-9DD8-6082A00E7BEB/User/p1&gt;\";\n})") </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload