我正在尝试备份然后恢复核心数据。我已经读过我需要保存sqlite文件然后在需要时加载它。我能够保存sqlite文件,然后加载回来但我的应用程序崩溃了。我明白,当我这样做时,我的持久存储可能完全没有了。我只是不知道如何让它知道我有一个新的sqlite文件并让它重新创建所有内容。
我试过了:
GroupAppDelegate *delegate = (GroupAppDelegate *)[[UIApplication sharedApplication] delegate];
// Delete Persistent Store
NSArray *stores = [[delegate persistentStoreCoordinator] persistentStores];
NSPersistentStore *store = [stores objectAtIndex:0];
NSError *error;
NSURL *storeURL = store.URL;
[[delegate persistentStoreCoordinator] removePersistentStore:store error:&error];
[[NSFileManager defaultManager] removeItemAtPath:storeURL.path error:&error];
// Load/Reset Persistent Store
[[delegate persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:nil];
NSManagedObjectContext *context = [delegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
// Load Sqlite file
NSString *filePath = [[[self applicationDocumentsDirectory] path] stringByAppendingPathComponent:@"Group.sqlite"];
[self.restClient loadFile:@"/Group App/Group.sqlite" intoPath:filePath];
所有这些的不同组合但无济于事。我是否需要在sqlite文件中保存其他内容?