coredata - 提供/设置默认数据

时间:2012-08-12 14:10:03

标签: ios core-data app-store

我在iOS App中使用coreData。用户可以添加,删除数据到数据库中。 我必须提供默认数据(一些不同的数据集)。 目前,我正在通过首次启动Application来创建数据库。我从csv文件中读取数据并用它创建数据库。 csv位于Application沙箱中; coreData(managedDocument)位于ApplicationDocument中(在运行时创建...)。

它对我来说很完美 - 但我问我,如果我将应用程序推送到AppStore,Apple会允许吗?

2 个答案:

答案 0 :(得分:2)

这种方法没有任何问题,也不能成为拒绝的理由。还有另一种方法可以做到这一点。您可以按照现在的方式创建数据库,复制.sqlite文件并将其作为默认数据库提供。然后在应用程序首次运行时复制它。以下代码将执行此操作:

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent: @"YourDBName.sqlite"];
    if (![fileManager fileExistsAtPath:storeURL.path]) {
        NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"YourDBName" ofType:@"sqlite"];
        if (defaultStorePath) {
            [fileManager copyItemAtPath:defaultStorePath toPath:storeURL.path error:NULL];
        }
    }

使用此方法,您无需在捆绑包中添加csv文件。

答案 1 :(得分:0)

是的,Apple确实允许运送默认填充的数据库。 标准的方法是在捆绑包中发送默认数据库,然后在启动时检查应用程序文档目录中是否有数据库,如果它不存在,则将数据库从捆绑包复制到文档目录