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'
Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.
As I mentioned earlier, the Core Data stack is the heart of Core Data. It's a collection of objects that make Core Data tick. The key objects of the stack are the managed object model, the persistent store coordinator, and one or more managed object contexts.
// 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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With