我使用 Xcode 4.3.3 中的 Master-Detail-Application 示例来开发数据查看器。该应用程序正在下载多个文件并将每个文件附加到表格视图中的一个单元格。一切正常。但是如果用户再次开始下载,我需要从数据库和 tableview 中删除所有以前的数据。我最终完成了以下步骤:
- 从协调器中删除旧商店
- 删除旧的 sqlite 数据库文件
- 将默认(空)sqlite db 文件从 bundle 目录复制到 doc 目录
- 更新商店协调员
- 更新表格视图。
但现在的问题是,如果我这样做,新数据不会写入 sqlite 文件。请,有人可以检查我的代码。以下代码位于 MasterViewController.m !我已经为此工作了好几天,我完全被困住了!!!
谢谢!
NSLog(@"Delete existing database file and copy default from bundle dir!");
NSFileManager* fileManager = [[NSFileManager alloc] init];
fileManager = [[NSFileManager alloc] init];
NSError *error = nil;
//get the documents directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// create database path documents dir
NSString *databasepath = [documentsDirectory stringByAppendingPathComponent:@"MyDatabase.sqlite"];
NSLog(@"Destination Database Path = %@", databasepath);
// create database path bundle dir
NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"MyDatabase.sqlite"];
NSLog(@"Recource Database Path = %@", resourceDBFolderPath);
// remove old store from coordinator
error = nil;
NSManagedObjectModel *managedObjectModel;
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"MyDatabase" withExtension:@"momd"];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]];
NSURL *applicationDocumentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *storeURL = [applicationDocumentsDirectory URLByAppendingPathComponent:@"MyDatabase.sqlite"];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
if (error != nil){
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
NSPersistentStore *persistentStore = [[persistentStoreCoordinator persistentStores] objectAtIndex:0];
[persistentStoreCoordinator removePersistentStore:persistentStore error:&error];
if (error)
if (error != nil){
NSLog(@"error removing persistent store %@ %@", error, [error userInfo]);
}
// Remove old database file
[[NSFileManager defaultManager] removeItemAtPath: [documentsDirectory stringByAppendingPathComponent: @"MyDatabase.sqlite"] error: &error];
if (error != nil){
NSLog(@"delete Database status: %@", [error localizedDescription]);
}
// copy default empty database file into documents dir
[fileManager copyItemAtPath: resourceDBFolderPath toPath: databasepath
error: &error];
if (error != nil){
NSLog(@"copy default Database status: %@", [error localizedDescription]);
}
// then update storecoordinator
// add clean store file back by adding a new persistent store with the same store URL
if (![persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType
configuration: nil
URL: storeURL
options: nil
error: &error]) {
NSLog(@"failed to add db file, error %@, %@", error, [error userInfo]);
}
NSLog(@"Database replacement end!");