• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Core Data fetch with to-many relationship
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Core Data fetch with to-many relationship


  • Subject: Re: Core Data fetch with to-many relationship
  • From: Ben Trumbull <email@hidden>
  • Date: Wed, 9 Sep 2009 00:44:24 -0700

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"
)}
----------

yup. Your print statement, though, is not for the objects you fetched, but their related books.


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"
)}
----------

You're not fetching books, you're fetching Owner. The predicate & fetch request pair together to say:


Fetch all the Owners that have ANY (one or more) object in their books relationship with a name like 'myPrefix*'

The log statement shows the first Owner in the results array has 2 books matching your predicate. It also has a bunch of others, but that's irrelevant to whether or not it matches the predicate. You seem to be thinking of this predicate

@"ALL books.name like 'myPrefix*'"

for which we don't currently generate SQL.

- Ben



_______________________________________________

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


  • Prev by Date: Cocoaheads Lake Forest (92630) meeting 9/8/2009 at 7 pm on Google Toolkit for Mac
  • Next by Date: Tab view customization
  • Previous by thread: Re: Core Data fetch with to-many relationship
  • Next by thread: KVO can be unsafe in -init?
  • Index(es):
    • Date
    • Thread