在我的应用程序中,我使用2个sqlite数据库,第一个用于读取,第二个用于写入和读取。我使用AppDelegate中的代码复制设备中的数据库:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self createCopyOfLolDatabaseIfNeeded];
[self createCopyOfSaveDatabaseIfNeeded];
}
- (void)createCopyOfLolDatabaseIfNeeded {
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appDBPath = [documentsDirectory stringByAppendingPathComponent:@"Lol.db"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Lol.db"];
success = [fileManager fileExistsAtPath:appDBPath];
if (success) {
return;
}
success = [fileManager copyItemAtPath:defaultDBPath toPath:appDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
- (void)createCopyOfSaveDatabaseIfNeeded {
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appDBPath = [documentsDirectory stringByAppendingPathComponent:@"Save.db"];
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Save.db"];
success = [fileManager fileExistsAtPath:appDBPath];
if (success) {
return;
}
success = [fileManager copyItemAtPath:defaultDBPath toPath:appDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
然后我在Copy Bundle Resources中添加了2个数据库。
我有2个问题: 1 - 如果我在Lol.db中修改某些东西并在捆绑中填充它,当我在模拟器或设备上启动应用程序时,应用程序将复制旧数据库而不是新数据库。 2-save.db不起作用,它被复制到模拟器中但是“坏了”,如果我手动将它放入模拟器的文件夹中我也可以在重启后使用它但是如果我重置模拟器并重新安装应用程序,save.db不起作用。
我该如何解决这个问题?
谢谢!