Problem while adding new Object to CoreData App

Posted by elementsense on Stack Overflow See other posts from Stack Overflow or by elementsense
Published on 2010-06-05T16:10:26Z Indexed on 2010/06/05 16:12 UTC
Read the original article Hit count: 265

Filed under:
|
|

Hi

Another Day, another CoreData problem,...but hopefully the last one now.

Ok here is a copy of what I have :

I have a List of Hotel Guests that stay in one Room and have Preferences. Once ready the user should select a guest and see the data and should also be able to add new guest, select the room (maintained also by application) and select their preferences (where the user can also add new preferences). The guest can have no or many preferences.

So here is what I have so far. I created 3 Entities : - Rooms with roomnumber - Preferences with name - GuestInfo with name -> with these Relationships room (Destination Rooms) and prefs (Destination Preferences with "To-Many Relationship") The prefs is a NSSet when you create a Managed Object Class.

Now I created a UITableViewController to display all the data. I also have an edit and add mode. When I add a new Guest and just fill out the name, everything works fine. But when I want to add the prefs or the room number I get this error :

Illegal attempt to establish a relationship 'room' between objects in different contexts

Now, what confuses me is that when I add a guest and enter just the name, save it, go back and edit it and select the prefs and room number it works ?

I have this line in both ViewControllers to select the room or prefs :

[editedObject setValue:selectedRoom forKey:editedFieldKey];

with this .h :

NSManagedObject *editedObject;
NSString *editedFieldKey;
NSString *editedFieldName;

Again, it works on the editing mode but not when I want to add a fresh object.

And to be sure, this is what I do for adding an new Guest :

- (IBAction)addNewItem 
{

    AddViewController *addViewController = [[AddViewController alloc] initWithStyle:UITableViewStyleGrouped];
    addViewController.delegate = self;
    addViewController.context = _context;

    // Create a new managed object context for the new book -- set its persistent store coordinator to the same as that from the fetched results controller's context.
    NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
    self.addingManagedObjectContext = addingContext;
    [addingContext release];

    [addingManagedObjectContext setPersistentStoreCoordinator:[[_fetchedResultsController managedObjectContext] persistentStoreCoordinator]];

    GuestInfo *info = (GuestInfo *)[NSEntityDescription insertNewObjectForEntityForName:@"GuestInfo" inManagedObjectContext:addingContext];

    addViewController.info = info;

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addViewController];

    [self.navigationController presentModalViewController:navController animated:YES];

    [addViewController release];
    [navController release];
}

Anything I have to do to initialize the Room or Prefs ? Hope someone can help me out. Thanks

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c