PDA

View Full Version : Need a little help with CoreData, insertNewObjectForEntityForName:




HungrySeacow
Apr 4, 2006, 06:55 PM
Anyone here think they can help me with this problem with CoreData? I need a method that will add an object to an entity, whose contentSet is bound to another entities selection. Right now I have:

NSManagedObject *source;
source = [NSEntityDescription insertNewObjectForEntityForName:@"secondEntity"
inManagedObjectContext: [self managedObjectContext]];

Unfortunately, this doesn't work because it is bound to the contentSet of another entity.



HungrySeacow
Apr 4, 2006, 10:34 PM
OK, I have created a sample program that demonstrates my problem:
http://www.hungryseacow.com/Personal/CoreDataTableView.zip

The Manual Add Method button for the Source managedObject works fine, However the one for the Results managedObject does not. Take a look at the methods in the __AppController.m file

- (IBAction)addResult:(id)sender
and
- (IBAction)addSource:(id)sender

Now if you look at the Results NSArrayController, you can see that it has two bindings, the contentSet is bound to the Source's selection. There must be some way to modify this method to get it to also work with the contentSet.


- (IBAction)addResult:(id)sender
{
NSLog(@"addResult called");
NSManagedObject *source;
source = [NSEntityDescription insertNewObjectForEntityForName:@"Result"
inManagedObjectContext: [self managedObjectContext]];
[[self managedObjectContext] processPendingChanges];

}

HungrySeacow
Apr 5, 2006, 03:43 PM
I don't know why I didn't think of this before! I fixed the problem. I created an IBOutlet to the NSArray Controller called resultArrayController and then sent it this message:

NSManagedObject *source;
[resultArrayController add: source];


EDIT: Sorry the solution that I had before was wrong, now it has been corrected.