[英]iPhone core data upsert from downloaded sqlite
I want to introduce the ability for the my core-data app to download a NEW sqlite file and update its stored data. 我想介绍一下我的核心数据应用程序能够下载新的sqlite文件并更新其存储的数据的功能。 Note the data model is not changing.
注意数据模型没有改变。
At first I am not worrying about user changes to the stored data, and simply want to overwrite it. 首先,我不必担心用户对存储数据的更改,而只想覆盖它。
I think this SO question falls short in answering these things. 我认为这个SO问题不足以回答这些问题。
Thanks for any guideance! 感谢您的指导!
If you want to Update any data base file without modifying existing data base then you have to implement like this 如果要更新任何数据库文件而不修改现有数据库,则必须执行以下操作
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self createEditableCopyOfDatabaseIfNeeded];
}
- (void)createEditableCopyOfDatabaseIfNeeded {
// First, test for existence - we don't want to wipe out a user's DB
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *documentDirectory = [self applicationDocumentsDirectory];
NSString *writableDBPath = [documentDirectory stringByAppendingPathComponent:@"DataBaseName.sqlite"];
BOOL dbexits = [fileManager fileExistsAtPath:writableDBPath];
if (!dbexits) {
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"DataBaseName.sqlite"];
NSError *error;
BOOL success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
}
I know this is an old question, but after tackling this myself I just wanted to add the following points: 我知道这是一个老问题,但是我自己解决了这个问题之后,只想补充以下几点:
My 2c, hope it helps someone. 我的2c,希望对您有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.