2014-04-24 43 views
1

我正在將一些細節存儲爲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提前

回答

1

你應該在的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(); 
} 
+0

我將上面的代碼粘貼到 - (NSPersistentStoreCoordinator *)persistentStoreCoordinator中。它將攔截應用程序。該應用程序無法正常工作。 – PSP

+1

你可以嘗試刪除你的舊應用程序?運行應用程序,複製文件SQLite文件,並使用這個新文件。 – nmh

+0

相同..沒有迴應 – PSP

0

的persistentStoreCoordinator方法禁用WAL模式也會改變這樣的: -

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]); 
     } 
    } 
相關問題