I am developing a backup / restore system for my application in which the user can download a backup copy of the main data store (sqlite file) and replace the current data store with the downloaded file. However, as soon as the user downloads the file and replaces the current data store, none of the data is updated. But when the application closes and restarts, recoverable data is available. How to make the application reload the master data store file?
I tried to access the application delegate from my UIViewController, which restores data, for example, to restore the underlying data stack and distributes it to all view controllers in the navigation stack:
MyAppDelegate *app = [[UIApplication sharedApplication] delegate];
app.managedObjectContext = nil;
app.persistentStoreCoordinator = nil;
app.managedObjectModel = nil;
managedObjectContext = [app managedObjectContext];
NSArray *controllers = [self.navigationController viewControllers];
UIViewController *c;
for (int i = 0; i < [controllers count]; i++) {
c = [controllers objectAtIndex:i];
[c setManagedObjectContext:managedObjectContext];
}
But this does not work, it only throws the following error when I return to the root view controller: 'The NSManagedObject with ID:0x5d79060 <x-coredata://D8E73D64-C9BA-4CFA-9213-F8BD61749155/MyObject/p2> has been invalidated.'
Does anyone know how to make the application reload data and start working with a new data warehouse file?
Jason source
share