I am using Core Data in my application.
My problem is that Entities (NSManagedObject) are persisting their state whether or not I call Save on NSManagedObjectContext, only condition is that the application is active. When I terminate application and again run it, all entities retains the last properties or changes made before the last "Save on NSManagedObjectContext" had been called.
Eg. I am having OpenViewController and FormOpenViewController.
FormOpenViewController works on (Form *) entity (NSManagedObject).
OpenViewController calls FormOpenViewController and fetch (Form *) object from database and passes it to FormOpenViewController's (Form *)form object.
In viewDidLoad of FormOpenViewController I change the value of one property "editable" of (Form *)form object to "YES".
i.e. FormOpenViewController.m
- (void)viewDidLoad
{
// changed the editable property of form to yes from no
if([formEditData.editable boolValue] == NO)
{
form.editable = [NSNumber numberWithBool:YES];
}
}
-(void)saveData
{
[[shareInstance delegate].managedObjectContext save:nil];
[shareInstance delegate].arrForder = [[ipegsClient shareInstance ] getLoginUser].folders.allObjects;
}
So now If I go back to OpenViewController without calling saveData method, and again come to FormOpenViewController then control does not go into if block, it means it form object is now having "editable" property as "YES" which should be "NO" because till I have not "saveData function".
UPDATE:
I am creating new instance of FormOpenViewController each time when I call it or present it. And also fetching new (Form *) form object each time from database before calling FormOpenViewController.
EDIT:
calling following function in applicationWillTerminate:
- (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
abort();
}
}
}
Does anyone know why this is happening?
Thanks in advance. correct me if I am going wrong somewhere.