6

I have a CoreData model with 4 entities.

Model screenshot -> http://img96.imageshack.us/img96/7857/screenshot20100209at182.png

State

-StateName

Location:

-locationName (attribute)

-locationDescription

-locationActivities (relatinship)

-state (relationship)

LocationActivities:

-location (relationship)

-activity (relationship)

Activities

-activityName(attribute)

-locationsActivities (relationship)

How can i write a query that selects all Locations that have

(activity = 'Golf' OR activity = 'Swimming') AND state = 'LA'

TechZen
  • 64,370
  • 15
  • 118
  • 145

3 Answers3

5
// With some NSManagedObjectContext *moc
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:@"Location"
                               inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:
                       @"(locationActivities.activity.activityName == %@ OR 
                          locationActivities.activity.activityName == %@) AND 
                         state.stateName == %@",
                       @"Golf", @"Swimming", @"LA"]];
NSError *error;
NSArray *results = [moc executeFetchRequest:request error:&error];

Basically, do a Core Data fetch as normal, then build the appropriate predicate to filter the results (as described in the Predicate Programming Guide).

Tim
  • 59,527
  • 19
  • 156
  • 165
  • what about the LocationActivities linking table? – Jonathan Ramirez Feb 09 '10 at 20:56
  • Fixed, but why is that entity there in the first place? You can (I believe) simply have a one-to-many relationship from Location to Activity. – Tim Feb 09 '10 at 21:01
  • this does not work. i get the error: 'to-many key not allowed here'. I have added another entity as suggested by http://stackoverflow.com/questions/1903177/coredata-many-to-many-relationships-and-nspredicate – Jonathan Ramirez Feb 09 '10 at 21:16
  • What predicate did you use that gave you that error? With a direct to-many relationship from Location to Activity (even if the inverse is also to-many), I think that you should be able to do "ANY activities.activityName == %@" as part of your predicate. – gerry3 Feb 09 '10 at 22:21
1

I just noticed that in your screen shot, your LocationActivities entity is actually spelled LocationAtivities (note the missing "c").

That is enough to wreck your graph. Any predicate that looks for LocationActivities will fail.

Errors like this make me hate programming. I seem to spend more time tracking down typos than I do fixing design errors.

TechZen
  • 64,370
  • 15
  • 118
  • 145
-1

As an aside, you need to stop thinking in terms of tables and queries for Core Data. In Core Data those specifics of only the sqllite persistent store and you never see them or deal with them.

Entities are not tables and relationships are not linking tables. Trying to cram the object model into an SQL in your head will lead you to grief because Core Data does not work like SQL.

TechZen
  • 64,370
  • 15
  • 118
  • 145