Reputation: 196
I am storing Some details to coredata as NSString.
NSEntityDescription *entityDesc=[NSEntityDescription entityForName:@"AddNewVehicle" inManagedObjectContext:self.managedObjectContext];
NSManagedObject *newObject=[[NSManagedObject alloc]initWithEntity:entityDesc insertIntoManagedObjectContext:self.managedObjectContext];
NSError *error;
NSString *inStr = [NSString stringWithFormat: @"%d", (int)x];
[newObject setValue:inStr forKey:@"slNo"];
[newObject setValue:txt_PlateName.text forKey:@"plateNumber"];
[newObject setValue:txt_vehicleName.text forKey:@"vname"];
[self.managedObjectContext save:&error];
Also I have two buttons named Backup Button and restore button.When i cilck on backup button, the Current core data must be duplicate as coredataBackup.sqlite. I was tried the following way:-
- (IBAction)savedata:(id)sender {
NSString * databaseName = @"coredata.sqlite";
NSString * databaseBackupName = @"coredataBackup.sqlite";
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *backupPath = [documentsDir stringByAppendingPathComponent:databaseBackupName];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:databaseName];
NSURL *toURL = [NSURL fileURLWithPath:backupPath];
if([fileManager fileExistsAtPath:backupPath])
{
NSLog(@"Checks for backuppath is present ?? Success");
//get rid of the old copy, only one allowed
[fileManager removeItemAtPath:backupPath error:&error];
}
if([fileManager fileExistsAtPath:dbPath])
{
if ([fileManager copyItemAtPath:dbPath toPath:backupPath error:&error]) {
NSLog(@"Creates backup");
[toURL setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];
} else {
NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}}
Result is it will creates the coredataBackup.sqlite file on Documents folder.But not copying current coredata details to new file. i don't know how to create back up for coredata.Please any one give some useful advice.Thanks in advance
Upvotes: 1
Views: 355
Reputation: 196
Also Change this :-
NSURL *MainURL = [NSURL fileURLWithPath:dbPath];
if([fileManager fileExistsAtPath:dbPath])
{
if ([fileManager copyItemAtPath:dbPath toPath:backupPath error:&error]) {
NSLog(@"Creates backup");
NSLog(@"Creates backup db path %@ backup path %@",dbPath,backupPath);
[[NSFileManager defaultManager] copyItemAtURL:MainURL toURL:toURL error:&error];
} else {
NSLog(@"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
Upvotes: 0
Reputation: 2503
You should disable WAL mode in persistentStoreCoordinator method of appDelegate
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary];
/*ATTETION: disable WAL mode*/
[pragmaOptions setObject:@"DELETE" forKey:@"journal_mode"];
NSNumber *optionYes = [NSNumber numberWithBool:YES];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
pragmaOptions, NSSQLitePragmasOption,
optionYes,NSMigratePersistentStoresAutomaticallyOption ,nil];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
abort();
}
Upvotes: 1