Question

I have a little statusbar app. It saves when I close the window, it saves when I quit the app but it isn't going to save every time I edit a row in my tableview or I add something to my arraycontroller. Is there a method to call saveAction at least every "enter" hit or when I confirm an edit? A save button is not what I'm searching for. Thanks in advance.

Was it helpful?

Solution 2

I would simply set your view controller as the delegate for both the textfield and textview. In an iOS environment you would add the protocol UITextFieldDelegate and UITextViewDelegate to your view controller header file and implement the methods - (void)textFieldDidEndEditing:(UITextField *)textField and - (void)textViewDidEndEditing:(UITextView *)textView for the UITextField and UITextView respectively.

As an alternative on a UITextField (iOS), there is a delegate method named - (BOOL)textFieldShouldReturn:(UITextField *)textField which is called whenever the 'enter' key is pressed on a UITextField.

In a Mac OSX environment you would add the appropriate protocol to your view controller header file (for NSTextView add the NSTextDelegate, for NSTextField add the NSControlTextEditingDelegate) and then implement the appropriate methods: -(void)textDidChange:(NSNotification *)notification for an NSTextView and - (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor for an NSTextField.

You can do any kind of validation you need to in those methods and then do a call to [myMOC save:&error]; before you return.

OTHER TIPS

This is my approach:

1) Create a subclass of NSManagedObject to add functionality for autosaving. You can substitute the line do { try managedObjectContext?.save() } catch { print(error) } by something like saveContext() if you have a global function defined elsewhere. Note that autosave is disabled by default.

class AutoSaveManagedObject: NSManagedObject {
    class var autosave: Bool { return false }
    var autosave: Bool?

    private var previousValue: AnyObject?

    override func willChangeValueForKey(key: String) {
        super.willChangeValueForKey(key)
        if ( autosave == true ) || ( autosave == nil && self.dynamicType.autosave ) {
            previousValue = valueForKey(key)
        }
    }

    override func didChangeValueForKey(key: String) {
        super.didChangeValueForKey(key)
        if ( autosave == true ) || ( autosave == nil && self.dynamicType.autosave ) {
            if "\(previousValue)" != "\(valueForKey(key))" {
                do { try managedObjectContext?.save() } catch { print(error) }
            }
            previousValue = nil
        }
    }
}

2) Make all core data objects subclasses of AutoSaveManagedObject rather than of NSManagedObject. If you want to enable autosaving, you should write something like this:

class MyMO: AutoSaveManagedObject {

    override class var autosave: Bool { return true }

    // Your @NSManaged vars here

}

3) Now all the instances of MyMO have autosave enabled. If you want to disable it for a certain instance, you can always write:

let myMO = ... as? MyMO
myMO?.autosave = false

Note that the instance's var autosave always has higher priority than the class var autosave, so you can set myMO?.autosave = nil in order to use the default autosave setting of the class.

When ever you edit a row within that method write this code

[[NSNotificationCenter defaultCenter] addObserver:self selector:(autosaveCoreData:) name:nil object:your_tableview_object];

-(void)autosaveCoreData:(Event*)event{

event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event"      inManagedObjectContext:_managedObjectContext];

[event setValue:Attribute_Value forKey:@"your atttribute"];
NSError *error;
if (![_managedObjectContext save:&error]) {

}
  }
}

I hope this may solve your problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top