Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help with many-to-many relationships in core data for iPhone

I have come to a roadblock in my current project. I basically have an app that is much like the Core Data Recipe app... Here is the basic structure I have in my .xcdatamodel

Entity: Restaurant String: name Category: category <---- to-many relationship

Entity: Category String: name Restaurant: restaurant <---- to-many relationship So basically, a Restaurant can have multiple categories... And there are a const number of predefined Categories.. For example: Restaurant: Name: Chili’s Categories: Take Out , Family Dining

“Take Out” and “Family Dining” are 2 of 10 different possible Restaurant Categories. How do I go about doing this.. I have looked at the sqllite database and I have my ZRestaurant and ZCategory table + the join table for both of them... I have data in both...

How do I go about setting my Restaurants Catagory with the different values? and then how do I fetch them back?

Thanks all! Kurt

like image 765
Kurt Avatar asked Sep 19 '09 21:09

Kurt


People also ask

What is Core Data on iPhone?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

Should I use Core Data iOS?

The next time you need to store data, you should have a better idea of your options. Core Data is unnecessary for random pieces of unrelated data, but it's a perfect fit for a large, relational data set. The defaults system is ideal for small, random pieces of unrelated data, such as settings or the user's preferences.

What is Core Data faults in iOS?

From the moment we ask for the value of a property of one of the records, Core Data jumps into action and fetches the data from the persistent store. This is better known as firing a Core Data fault.

What are relationships in Core Data?

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.


1 Answers

OK, After working on this for the past 2 days I finally came up with my solution which was actually a mix between Alex and Wills suggestions... Thank you to both of you!!

Here is what I have...

 NSManagedObjectContext *context = [restaurant managedObjectContext];


NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:context]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:&sortDescriptor count:1];
[fetchRequest setSortDescriptors:sortDescriptors];

NSError *error = nil;
NSArray *possibleCategories = [context executeFetchRequest:fetchRequest error:&error];

categoryArray = [[NSMutableArray alloc] initWithArray:possibleCategories];

currentCategories = [restaurant valueForKeyPath:@"categories"];

[restaurant addCategoriesObject:(Category *)[possibleCategories objectAtIndex:15 ]];

[currentCategories addObject:(Category*)[categoryArray objectAtIndex:15]];

and then I save like this

- (void)save{
    NSLog(@"EditCatagoriesTableViewController - save");

    NSSet* myCategorySet = [[NSSet alloc] initWithSet:currentCategories];

    NSError *error = nil;

    [restaurant addCategories:myCategorySet];


    error = nil;
    if (![restaurant.managedObjectContext save:&error]) {
        // Handle error
        NSLog(@"restaurant - Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }       
}

And that does it!

Thank you so much for the help you two!!!

-Kurt

like image 96
Kurt Avatar answered Nov 13 '22 15:11

Kurt