Dim*_*ris 9 iphone schema persistence core-data objective-c
我有一个从互联网上获取数据的应用程序,并使用CoreData将它们存储在设备中,以获得更流畅的体验.
因为我使用Core Data,所以每当我的架构发生变化时,当我尝试使用存储在设备上的先前数据运行它时,应用程序会崩溃.什么是检测此更改并从设备中擦除所有数据的最快方法,因为我不介意重新加载它们.它击败了崩溃并将模式重新映射到新模式(在我的例子中).
我看到这个检查是在getter中执行的:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
Run Code Online (Sandbox Code Playgroud)
所以我只需要知道实施擦除整个数据库和重新设置核心数据的方法.谢谢 :)
Dim*_*ris 14
回到这个问题,要删除我的CoreData存储中的所有数据,我决定简单地删除sqlite数据库文件.所以我刚刚实现了NSPersistentStoreCoordinator这样的:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator != nil) {
return persistentStoreCoordinator;
}
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"myAppName.sqlite"]];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(@"Error opening the database. Deleting the file and trying again.");
//delete the sqlite file and try again
[[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
//if the app did not quit, show the alert to inform the users that the data have been deleted
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error encountered while reading the database. Please allow all the data to download again." message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
}
return persistentStoreCoordinator;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2398 次 |
| 最近记录: |