I have no 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;
}
The managed object class definition:
#import <Foundation/Foundation.h>
typedef enum {
UISwitchParameterIdGpsSimulatorEnable,
UISwitchParameterIdIllegal
}UISwitchParameterId;
@interface SystemParameters : NSManagedObject {
// NSManagedObjectContext *mManagedObjectContext;
////
// GPS SIMULATOR
////
BOOL mGpsSimulatorEnabled;
NSString *mGpsSimulatorFileName;
NSInteger mGpsSimulatorPlaybackSpeed;
}
@property (nonatomic, assign) BOOL mGpsSimulatorEnabled;
@property (nonatomic, retain) NSString *mGpsSimulatorFileName;
@property (nonatomic, assign) NSInteger mGpsSimulatorPlaybackSpeed;
+ (SystemParameters *)sharedInstance;
- (void) saveToCoreData;
@end
There is no error but the sqLite file is not updated, hence the data is not persistent.
Thanks in advance.