0

I have an NSArrayController that is connected to my Entity in Core Data. I have an IBOutlet attached to the NSArrayController so that I can add objects from my Delegate.

I am trying to add an Object to the NSArrayController with a some presets. So far I have found out how to add an object using.

[cards add:sender];

This works but it adds a blank object. When I exit out of the app it saves this object to the Entity, so that when I hope it again it is still there.

But I also found a way to create a new object with presets, but it doesn't save if like the last method did.

NSDictionary *addedObject = [NSDictionary dictionary];
addedObject = [NSDictionary dictionaryWithObjectsAndKeys:@"TITLE", @"title",
@"CONTENT", @"content", nil];
[cards addObject:addedObject];

Are there any other ways to create a new object with presets but make it so it saves as well? I am new to Core Data, so I'm not 100% on all of this. Maybe there is a way to create a pointer to the actual Entity and edit it from there?

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
benj9a4
  • 101
  • 2
  • 8

3 Answers3

3

Either subclass NSArrayController and re-implement the add function, or subclass your entity and re-implement awakeFromInsert. Both are acceptable places for preset object values.

In the case of NSArrayController subclassing:

@implementation CardsArrayController

-(void) add:(id)sender {
    NSManagedObject *newItem = [self newObject];
    //do object set up here...
    [self addObject:newItem];
}

@end
Khakionion
  • 742
  • 7
  • 20
  • How would you suggest I re-implement the NSArrayController's add function, sorry I'm new to Cocoa. – benj9a4 Mar 11 '13 at 17:07
  • I added the example, but in your question, you're using NSDictionary. Be sure you understand that NSArrayController needs NSManagedObjects when dealing with Core Data entities. – Khakionion Mar 11 '13 at 21:52
  • Do I need to connect this NSManagedObject to My Entity, and if so how do I do that? I have added my values into the NSManagedObject as so.. `- (void) add:(id)sender { NSManagedObject *newItem = [self newObject]; [newItem setValue:@"Happy Birthday" forKey:@"title"]; [newItem setValue:@"Content" forKey:@"content"]; [newItem setValue:@"sender" forKey:@"sender"]; [self addObject:newItem]; }` – benj9a4 Mar 14 '13 at 00:58
  • If your NSArrayController has its entity name set already, you should be okay. – Khakionion Mar 15 '13 at 18:38
  • If this answered your question, I'd love it to be marked as the answer. Thanks! – Khakionion Nov 10 '13 at 07:11
  • is it OK to directly call the [NSArrayController addObject:preConfiguredNewEntityObject]; instead of overriding the add: action? what are the consequences? What does "add:" do in addition to addObject: ? – Motti Shneor Nov 09 '15 at 08:05
0

Whichever way you add the object getting the managedObjectContext reference and saving the context will save your objects.

If you are using the boiler plate code provided by apple then you can use the following code:

NSError *error;
AppDelegate *appD = [NSApp delegate];
[appD.managedObjectContext save:&error];

or you could call [appD saveAction:Nil] . This also tries to commit editing before saving the context.

or you could hook up a button to the saveAction: (it's an IBAction) to save the context on a button click.

EDIT: Also the object created from array controller need not be blank. It's upto you. You can use bindings from textfields (or other editable ui elements) to the arrayController to do this. Or you could get the content property of the array to access the object and then edit it. Or you could add the whole object through code without using array controller at all. There are many ways.

Rakesh
  • 3,370
  • 2
  • 23
  • 41
  • thats probably because you are not creating an NSManagedObject of that entity. With the dictionary you are just randomly creating an object with a key and a value. For your object to be saved in core-data you need to create it using an entity description with respect to a particular managed object context. All this is done for you automatically when you use the add: method of the arrayController. – Rakesh Mar 11 '13 at 19:47
  • i.e if you have setup the array controller properly. – Rakesh Mar 11 '13 at 19:54
0

again, an NSDictionary - is NOT an entity instance. Entities are represented in memory as instances of NSMangedObject (or subclasses of it, but no real need to subclass).

Indeed managed objects support KVC, so they could behave like dictionaries for certain things (setValue:forKey etc.) but that's where similarity ends. an NSDictionary cannot be inserted into an NSArrayController that controls Core-Data entities. you must create the entity using Core-Data APIs

e.g.

NSManagedObject *myNewEntiry = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:self.managedObjectContext];

If you create your entity this way - using the same managed object context used by your NSArrayController - it will be added automatically (but not immediately) to the array controller's content.

The NSArrayController IBAction named -(void) add:(id)sender; is a convenience shortcut that indeed creates a new entity of the type (and name) designated to the NSArrayController, and adds it to the controllers content. But indeed - as it does not receive any parameters - it creates a "blank" entity. Blank means - with default values as defined in the model.

First answer in this page, rightfully suggests that you override the add: IBAction in your own NSArrayController subclass, to create and setup your new entity before inserting it to the NSArrayControllers' content.

so It would look like:

@implementation CardsArrayController

-(void) add:(id)sender {
     NSManagedObject *myNewEntity = [NSEntityDescription insertNewObjectForEntityForName:@"MyEntityName" inManagedObjectContext:self.managedObjectContext];
    //do object set up here...
    [myNewEntity setValue@"Happy Birthday" forKey:@"title"];
    [self addObject:newItem];
}

@end

I hope this clarifies stuff a little, but you'll need to read about core-data and browse some apple sample-code for Core-Data based application.

Motti Shneor
  • 2,095
  • 1
  • 18
  • 24