Core Data Sqlite restore
I'm trying to backup and then restore my core data. I've read that I need to save the sqlite file and then load it back when I need it. I'm able to save the sqlite file and then load it back but then my app crashes. I understand that my persistent store is probably all out of whack when I do this. I just don't know how to get it to know I have a new sqlite file and have it recreate everything.
I've tried:
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 = [NSEn开发者_JS百科tityDescription 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];
And different combinations of all of this but to no avail. Do I need to save something else along with the sqlite file?
精彩评论