0
votes

Background:   I built an app w/ inexperience. It uses bindings, a NSArrayController subclass and Core Data/iCloud. The experience level caused a lot of code unnecessary writing, yet the app worked ok. A button linked to the NSArrayController add: method triggered newObject and addObject:. Removing never worked and led me to look at contentArray binding . A week ago, I began cleaning the app to more thoroughly use the simplicity of bindings. (Unfortunately, complexity also entered into this problem.)

Assumptions:   1) add: and remove: methods would automagically fire the methods to create and delete objects. Many simple tutorials seem to work this way. 2) The add:, addObject:, remove: and removeObject: methods do not need to be overridden.

Problem:   addObject: and removeObject: do not get called (after some point of change) unless they are explicitly called in the add: or remove: method. I have never had the functionality of deleting both the array controller and managed object with a simple click on a remove button.

Question:   What is required for the Core Data managed objects to be added and removed?

Other info:   The array controller pictured below has Custom Class set to CheckinArrayController. The Core Data stack is initialized in AppDelegate. The newObject method has been overridden to preset some attributes.

EDIT: Override in array controller subclass add:, newObject, addObject:, and arrangeObjects:

Code in Array Controller (NSArrayController subclass)

@implementation CheckinArrayController

- (id)newObject {
    id newObject = [super newObject];
    //  do object set up here ....
    return newObject;
}

- (void)add:(id)sender {
    /*  without the next 2 no object is added, although this is called
        NSManagedObject *newItem = [self newObject];
       [self addObject:newItem];    */
}

- (void)addObject:(id)object {
    [super addObject:object];
}

- (void)remove:(id)sender {
    [super remove:sender];
}

- (void)removeObject:(id)object {
    [super removeObject:object];
}

@end

Current array controller connections:

enter image description here

1
Did you put the array controller in Entity Mode?Willeke
Yes. With the entity name 'Checkin' (from data model) and Prepares Content checked.David
Btw if you bind Selection Indexes of the table view to the array controller, you can bind enabled of the buttton to canRemove of the array controller. Same for the add button and canAdd.Willeke
Which methods did you override?Willeke
@Willeke See edit to OP.David

1 Answers

0
votes

I would suggest creating a convenience init in your ManagedObject class. Here is another SO question that outlines this:

Designated Init for Managed Object

Call this init method when you want to create a new Managed Object. Your arraycontroller has a managedObjectContext from the bindings you have set up.

I use it like this:

let entity = NSEntityDescription.entity(forEntityName: "yourEntityName", in: managedObjectContext!)
let object = YourManagedObjectClassName(property1: "property1 entity: entity!, insertIntoManagedObjectContext: managedObjectContext)

I use the stock removeObject methods in the NSArrayController base class to delete objects.

removeObject Documentation