Fail to save a managed object to core-data after its properties were updated.

Posted by Tzur Gazit on Stack Overflow See other posts from Stack Overflow or by Tzur Gazit
Published on 2010-05-06T14:11:08Z Indexed on 2010/05/06 15:08 UTC
Read the original article Hit count: 164

Filed under:
|
|

I have to trouble to create the object, but updating it fails. Here is the creation code:

 // Save data from pList to core data fro the first time
- (void) saveToCoreData:(NSDictionary *)plistDictionary {

// Create system parameter entity   
SystemParameters *systemParametersEntity = (SystemParameters *)[NSEntityDescription
                                                                insertNewObjectForEntityForName:@"SystemParameters" 
                                                                inManagedObjectContext:mManagedObjectContext];

////
// GPS SIMULATOR
////
NSDictionary *GpsSimulator = [plistDictionary valueForKey:@"GpsSimulator"];

[systemParametersEntity setMGpsSimulatorEnabled:[[GpsSimulator objectForKey:@"Enabled"] boolValue]];
[systemParametersEntity setMGpsSimulatorFileName:[GpsSimulator valueForKey:@"FileName"]];
[systemParametersEntity setMGpsSimulatorPlaybackSpeed:[[GpsSimulator objectForKey:@"PlaybackSpeed"] intValue]];

[self saveAction];  
}

During execution the cached copy is changed and then it is saved (or trying) to the database. Here is the code to save the changed copy:

// Save data from pList to core data fro the first time
- (void) saveSystemParametersToCoreData:(SystemParameters *)theSystemParameters {

// Step 1: Select Data
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"SystemParameters" 
                                          inManagedObjectContext:mManagedObjectContext];
[fetchRequest setEntity:entity];

NSError *error = nil;
NSArray *items = [self.managedObjectContext
                  executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

if (error) {
    NSLog(@"CoreData: saveSystemParametersToCoreData: Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

// Step 2: Update Object
SystemParameters *systemParameters = [items objectAtIndex:0];

////
// GPS SIMULATOR
////
[systemParameters setMGpsSimulatorEnabled:[theSystemParameters mGpsSimulatorEnabled]];
[systemParameters setMGpsSimulatorFileName:[theSystemParameters mGpsSimulatorFileName]];
[systemParameters setMGpsSimulatorPlaybackSpeed:[theSystemParameters mGpsSimulatorPlaybackSpeed]];



// Step 3: Save Updates
[self saveAction];
}

As to can see, I fetch the object that I want to update, change its values and save. Here is the saving code:

- (void)saveAction {

NSError *error;
if (![[self mManagedObjectContext] save:&error]) {
    NSLog(@"ERROR:saveAction. Unresolved Core Data Save error %@, %@", error, [error userInfo]);
    exit(-1);
}
}

The Persistent store method:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

if (mPersistentStoreCoordinator != nil) {
    return mPersistentStoreCoordinator;
}

NSString    *path = [self databasePath];
NSURL *storeUrl = [NSURL fileURLWithPath:path];

NSError *error = nil;
mPersistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![mPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

return mPersistentStoreCoordinator;
}

There is no error but the sqLite file is not updated, hence the data is not persistent.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about core-data