Search code examples
xcodensmanagedobjectnsmanagedobjectcontextnsfetchrequest

Saving changes to a NSManagedObject


I have a problem with making changes to ManagedObjects and saving those changes to the persistent store.

What does work is deleting objects, inserting object. and fetching those objects. As said, i fail making a change to a fetched managed object.

I have two view controllers. Both have a public property:

@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;

The AppDelegate.m file sets this property for the first view controller in the application:didFinishLaunchingWithOptions: method like so:

// get reference to view controller //
//..
controller1.managedObjectContext = self.managedObjectContext;

view controller1 again passes the managedObjectContext when pushing to the second view controller, it does this in the prepareForSegue: method like so:

// Get reference to the detail view //
MySecondViewController *controller2 = segue.destinationViewController;        
// Pass the managed object context //
controller2.managedObjectContext = self.managedObjectContext;

Within this second view controller I fetch objects from core data and I store them in a property of the view controller like this:

@property (nonatomic, strong) MyKindOfObject *object;
...
self.object = [fetchResults lastObject];

Fetching objects seems to work fine as i nicely get results..

The fetched object has a number of properties, one of them is of type NSString. The value of this property I show in a UITextField. The user can change the value here and when done he presses a button. I catch the action and I do the following:

self.object.mytext = textField.text;

followed by trying to save to core data:

// Save to CoreData //
NSError *error = nil;
if(![self.managedObjectContext save:&error]){
    // handle the error //
    NSLog(@"is error");
}
else{
    NSLog(@"no error");
}

1) The next time the user returns to this view controller, again the object will be fetched. But it still has the old value.

2) Also I use a Firefox add-on called SQLite Manager to keep an eye on the data within the related sqlite file. When stepping through the code, after calling the save method nothing changes in the file.

3) Also within Xcode i use the Variables View to keep an eye on the self.managedObjectContext object. When I am storing the new data into my object, right before calling save, none of the properties of self.managedObjectContext change (like unprocessedChanges or changedObjects).

4) I also added a call to the hasChanges method of the managedObjectContext, but this returns NO:

if([self.managedObjectContext hasChanges]){
   NSLog(@"changes managed object context!");
}

5) I also added a call to the hasChanges method of the related managedObject, but this also returns NO:

if([self.object hasChanges]){
    NSLog(@"changes in managed object!");
}

I am probably doing something totally wrong here but I can not figure out what it is. I truly hope somebody can help me out?. Thanks in advance!


Solution

  • Oke let me answer my own question: as the newby that I am, i indeed did something totally wrong. I was thinking I knew better then apple themselves. lesson learned: I do not!

    After creating entities within the model I created custom managed object classes for each entity (or actually NSManagedObject subclasses for each entity). Within the generated implementation files all properties (attributes) are implemented as @dynamic. Due to lack of knowledge I did not understand/recognise this. So the newby in me thought: lets change all that to @synthesize.

    Hopefully this post can help some other newby to not make the same mistake.