Re: Core Data fetch with to-many relationship
Re: Core Data fetch with to-many relationship
- Subject: Re: Core Data fetch with to-many relationship
- From: Alex Reynolds <email@hidden>
- Date: Wed, 9 Sep 2009 00:11:44 -0700
I think I found a solution:
NSArray *testFetchResults = [managedObjectContext
fetchObjectsForEntityName:@"Owner" withPredicate:[NSString
stringWithFormat:@"ANY books.name beginswith 'myPrefix'"]];
NSSet *filteredSet = [(NSSet *)[(Owner *)[testFetchResults
objectAtIndex:0] buckets] filteredSetUsingPredicate:[NSPredicate
predicateWithFormat:@"name beginswith 'myPrefix'"] );
NSLog(@"%@", [filteredSet description]);
----------
{(
Book <
name: myPrefix-v1
creationDate: 2008-11-10 00:02:35 -0800
Owner < 123456 | username >
>,
Book <
name: myPrefix-v2
creationDate: 2008-11-09 23:58:36 -0800
Owner < 123456 | username >
>
)}
----------
-Alex
On Sep 8, 2009, at 11:04 PM, Alex Reynolds wrote:
Hi Melissa,
Thanks for the name change suggestion.
When I log the test fetch results:
NSArray *testFetchResults = [managedObjectContext
fetchObjectsForEntityName:@"Owner" withPredicate:[NSString
stringWithFormat:@"ANY books.name like 'myPrefix*'"]];
NSLog([[(Owner *)[testFetchResults objectAtIndex:0] books]
valueForKey:@"name"]);
I get the following sample output:
----------
{(
"foo",
"bar",
"myPrefix-v1",
"myPrefix-v2"
)}
----------
These were the four test Book objects I originally associated with
one Owner, and then saved to the managed object context.
I'm unclear why the predicate statement:
@"ANY books.name like 'myPrefix*'"
would return all books, whether their name starts with 'myPrefix' or
not. The output I would expect is:
----------
{(
"myPrefix-v1",
"myPrefix-v2"
)}
----------
Should I be using a different predicate to retrieve Book objects
with the names "myPrefix-v1" and "myPrefix-v2"?
Regards,
Alex
On Sep 8, 2009, at 7:08 PM, Melissa J. Turner wrote:
On Sep 8, 2009, at 18:03, Alex Reynolds wrote:
Relationship fault for (<NSRelationshipDescription: 0x3d1b2b0>),
name Book, isOptional 1, isTransient 0, entity Owner,
renamingIdentifier Book, validation predicates (
), warnings (
), versionHashModifier (null), destination entity Book,
inverseRelationship Owner, minCount 0, maxCount 0 on Owner <
123456 | username >
That's not an error, it's an unfired to-many relationship fault. If
you call a method on it (say [testFetchResults
valueForKey:@"name"]) it will fire and look more like what you're
expecting.
BTW, it's generally a bad idea to use uppercase key names. The ivar
'Book' should probably be 'books', and 'Owner' should be
'owner' (the class names are fine the way they are).
+Melissa
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden