我正在將一些細節存儲爲coredata作爲NSString。當我們點擊一個UIButton時如何複製sqlite數據庫?
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];
我也有一個名爲備份按鈕兩個按鈕和恢復button.When我依序按一下[備份按鈕,當前的核心數據必須是重複的coredataBackup.sqlite。我嘗試以下方法: -
- (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]);
}
}}
結果是它會創建coredataBackup.sqlite文件上的文檔folder.But不是抄襲電流coredata細節,以新的文件。我不知道如何創建備份的coredata.Please任何一個可以提供一些有用advice.Thanks提前
我將上面的代碼粘貼到 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator中。它將攔截應用程序。該應用程序無法正常工作。 – PSP
你可以嘗試刪除你的舊應用程序?運行應用程序,複製文件SQLite文件,並使用這個新文件。 – nmh
相同..沒有迴應 – PSP