Need a little help with CoreData, insertNewObjectForEntityForName:

Discussion in 'Mac Programming' started by HungrySeacow, Apr 4, 2006.

  1. macrumors regular

    Joined:
    Jan 11, 2006
    Location:
    West Palm Beach
    #1
    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:
    Code:
    NSManagedObject *source;
    source = [NSEntityDescription insertNewObjectForEntityForName:@"secondEntity"
                 inManagedObjectContext: [self managedObjectContext]]; 
    
    Unfortunately, this doesn't work because it is bound to the contentSet of another entity.
     
  2. thread starter macrumors regular

    Joined:
    Jan 11, 2006
    Location:
    West Palm Beach
    #2
    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.

    Code:
    - (IBAction)addResult:(id)sender
    {
    	NSLog(@"addResult called");
    		NSManagedObject *source;
    		source = [NSEntityDescription insertNewObjectForEntityForName:@"Result"
    												  inManagedObjectContext: [self managedObjectContext]];
    		[[self managedObjectContext] processPendingChanges];
    		
    }
    
     
  3. thread starter macrumors regular

    Joined:
    Jan 11, 2006
    Location:
    West Palm Beach
    #3
    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:
    Code:
    	NSManagedObject *source;
    	[resultArrayController add: source];
    
    EDIT: Sorry the solution that I had before was wrong, now it has been corrected.
     

Share This Page