Core Data: Need help with fetch request

  • I want to fetch some objects based on a condition existing in one of
    its relationships.

    Example Object Model:

    Car -> Wheel

    A car can have one or more wheels.  And a wheel has a property called
    color (to keep it simple an NSString). The relationship is called
    wheels. There is no inverse relationship.
    What I want is to retrieve all cars that a color that contains the
    word 'black'.

    Code I've tried so far:

    NSEntityDescription *entityDescription = [NSEntityDescription
    entityForName:@"Car" inManagedObjectContext:context];
    NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
    [request setEntity:entityDescription];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString
    stringWithFormat:@"ANY wheels.color like '*black*'] ];
    [request setPredicate:predicate];
    NSArray *array = [context executeFetchRequest:request error:nil];

    Unfortunately the fetch request does result in any objects retrieved.
    I am using an SQL-lite store for this example.

    Kind regards,

    Diederik
  • On 10/10/2006, at 21.27, Diederik Hoogenboom wrote:

    > I want to fetch some objects based on a condition existing in one
    > of its relationships.
    >
    > Example Object Model:
    >
    > Car -> Wheel
    >
    > A car can have one or more wheels.  And a wheel has a property
    > called color (to keep it simple an NSString). The relationship is
    > called wheels. There is no inverse relationship.

    What is your really, really good reason for not having an inverse
    relationship?
    Is it one-to-many or many-to-many?

    > Unfortunately the fetch request does result in any objects retrieved.
    > I am using an SQL-lite store for this example.

    What does the generated SQL look like?
    Does it change if you add an inverse relationship?
  • Jakob,

    When I designed the model I though I would never have to go the other
    way (in the example from Wheels to Cars).  I can add the relationship
    and then it  would probably work. Unfortunately, I now have to
    convert the whole persistent store from the old model to the new
    model just for one relationship!  If I add the relationship the old
    store will complain that the field is unkown (eventough it's set to
    'optional'. Oh well, that's what you get from not following the
    rules ;-).

    Diederik

    On 14-okt-2006, at 17:17, Jakob Olesen wrote:

    >
    > On 10/10/2006, at 21.27, Diederik Hoogenboom wrote:
    >
    >> I want to fetch some objects based on a condition existing in one
    >> of its relationships.
    >>
    >> Example Object Model:
    >>
    >> Car -> Wheel
    >>
    >> A car can have one or more wheels.  And a wheel has a property
    >> called color (to keep it simple an NSString). The relationship is
    >> called wheels. There is no inverse relationship.
    >
    > What is your really, really good reason for not having an inverse
    > relationship?
    > Is it one-to-many or many-to-many?
    >
    >> Unfortunately the fetch request does result in any objects retrieved.
    >> I am using an SQL-lite store for this example.
    >
    > What does the generated SQL look like?
    > Does it change if you add an inverse relationship?
    >
    >
  • On 15/10/2006, at 14.02, Diederik Hoogenboom wrote:

    > When I designed the model I though I would never have to go the
    > other way (in the example from Wheels to Cars).  I can add the
    > relationship and then it  would probably work.

    You should test this first, your fetching problem may be unrelated to
    the missing inverse relationship.

    > Unfortunately, I now have to convert the whole persistent store
    > from the old model to the new model just for one relationship!  If
    > I add the relationship the old store will complain that the field
    > is unkown (eventough it's set to 'optional'. Oh well, that's what
    > you get from not following the rules ;-).

    The rule is: You should have a really, really good reason for
    creating a unidirectional relationship, and you need to know what you
    are doing.
  • On 15/10/2006, at 17.06, Marc Respass wrote:
    >
    > I have a question about this. Coming from WebObjects/EOF, I do not
    > include inverse relationships if I don't need them which is reason
    > enough not to. You ask why not but I'm wondering if it is
    > recommended to always include an inverse -- I do it in CoreData
    > just to get rid of the warning bu it I read that warning and I'm
    > like "I know. 'Cause I didn't make one 'cause I don't need one."
    > but the compiler doesn't care what I think :)

    Core Data uses the inverse relationships to maintain referential
    integrity. I suppose EOF can use foreign keys for that.

    Is there any advantage to using unidirectional relationships?
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today