简体   繁体   English

从下载的sqlite中获取iPhone核心数据更新

[英]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 am finding the only way the stored data is updating is to change the name of the sqlite file.. ? 我发现存储数据更新的唯一方法是更改​​sqlite文件的名称。
  • Is there a standatd way to merge, via core-data, 2 sqlite files? 是否有一种可靠的方式可以通过核心数据合并2个sqlite文件? Thus keeping user modified data. 从而保留用户修改的数据。
  • Can an update (downloaded sqlite file) be a subset of the shipped core data sqlite? 更新(下载的sqlite文件)可以作为附带的核心数据sqlite的子集吗? Note the sqlite contains binary information. 请注意,sqlite包含二进制信息。

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: 我知道这是一个老问题,但是我自己解决了这个问题之后,只想补充以下几点:

  • The default sqlite file for a core data store, yourProjectName.sqlite, seems to be protected. 核心数据存储的默认sqlite文件yourProjectName.sqlite似乎已受到保护。 Overwriting it doesn't work. 覆盖它不起作用。 You will need to do what raaz suggested above and create a copy. 您将需要执行raaz上面建议的操作并创建一个副本。
  • After creating the copy you can overwrite the file easily. 创建副本后,您可以轻松覆盖文件。 However, some suggestions in other answers show that the best way to overwrite the core data sqlite file is to delete it first. 但是,其他答案中的一些建议表明,覆盖核心数据sqlite文件的最佳方法是先删除它。 This doesn't work on the device! 这在设备上不起作用! It throws an error saying it couldn't remove the file. 抛出错误,提示它无法删除文件。 It does work in the simulator, but not on the device (tested on a 3G on 4.2) 它可以在模拟器中运行,但不能在设备上运行(在4.2的3G上测试)
  • So the best way is to just overwrite the file rather than deleting it and creating a new one. 因此,最好的方法是只覆盖文件,而不是删除它并创建一个新文件。
  • And of course be sure to remove the store from the persistent store coordinator first! 当然,一定要先从持久性存储协调器中删除该存储!

My 2c, hope it helps someone. 我的2c,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM