Reputation: 31
I am letting the user backup and restore the sqlite database through email on the iOS device. I export and import the sqlite database successfully. My issue is when I want to replace the current sqlite file with the new file the data does not update automatically. When I add an exit(0) at the end of the function it closes and the data is there when the app is reopened. This is not a solution as it is basically a crash. Here is my function that I use to replace one sqlite file with the other. Note I have turned journaling off so the whole database is one sqlite file Does anybody know a correct way of replacing one sqlite file with the other while the app is running? Thank you.
- (BOOL)resetApplicationModel {
// ----------------------
// This method removes all traces of the Core Data store and then resets the application defaults
// ----------------------
NSError *_error = nil;
NSURL *_storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Database.sqlite"];
NSPersistentStore *_store = [persistentStoreCoordinator persistentStoreForURL:_storeURL];
// Remove the SQL store and the file associated with it
if ([persistentStoreCoordinator removePersistentStore:_store error:&_error]){
[[NSFileManager defaultManager] removeItemAtPath:_storeURL.path error:&_error];
}
if (_error) {
NSLog(@"Failed to remove persistent store: %@", [_error localizedDescription]);
return NO;
}
[persistentStoreCoordinator release], persistentStoreCoordinator = nil;
[managedObjectContext release], managedObjectContext = nil;
NSString *applicationDocumentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *originalFile = [applicationDocumentDirectory stringByAppendingPathComponent:@"Database.sqlite"];
NSString *backupDatabaseFolder = [applicationDocumentDirectory stringByAppendingPathComponent:@"Backups"];
NSString *backupFile = [backupDatabaseFolder stringByAppendingPathComponent:[NSString stringWithFormat:@"DatabaseBackup.sqlite"]];
[[NSFileManager defaultManager] copyItemAtPath:backupFile toPath:originalFile error:nil];
// Rebuild the application's managed object context
rootViewController.managedObjectContext = nil;
rootViewController.managedObjectContext = self.managedObjectContext;
return YES;
}
Upvotes: 0
Views: 185
Reputation: 70976
I'm going to assume that your managedObjectContext
and persistentStoreCoordinator
methods follow the typical approach-- i.e. that after you set them to nil, asking for them again will lazily reinitialize them. If that's not the case, then that's the first problem, that you're tearing down the old Core Data stack but not replacing it.
Assuming that's true, the problem is-- as you said in a comment-- that your app makes no attempt to fetch new data. If you want the app to display new data.... you need to have your app ask for that data using a fetch request. Your UI won't automatically update just because you ran the method above, you need to do something to make it fetch the new values.
Doing that means that you need to clear out every object you've fetched from Core Data-- every managed object plus any fetched results controllers you may have.
Upvotes: 1