使用 iOS 应用传送核心数据

Shipping core data with iOS app

我正在开发一个字典应用程序,其中包含大约 10,000 个单词。我正在使用核心数据来存储所有数据,我使用的语言是 swift.

我只想将此数据与应用程序一起发送,以便从应用程序商店下载应用程序时,它包含所有数据。

我已经对此进行了搜索,发现可以通过将 SQLite 文件包含到项目中来完成。但真的不知道该怎么做,El Capitan 中的模拟器目录在哪里。

我只是 iOS 的初学者,所以请有人用非常简单的步骤向我解释一下。

  1. 将 sqlite 文件添加到项目中。
  2. 你会打电话给

     [[NSFileManager defaultManager] copyItemAtURL:preloadURL toURL:storeURL error:&error]
    

之前:

    [_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                       configuration:nil
                                                                 URL:storeURL
                                                             options:nil
                                                               error:&error]

其中 preloadURL 将是应用程序包中文件的 URL。 storeURL 是核心数据的 sqlite 文件的路径(通常在应用程序目录中)。

所以如果文件不存在,文件将从 bundle 复制到 app 目录,否则失败。

这可能是一个很长的答案。最好将其作为教程进行解释。以下 link 将向您展示该怎么做。

http://www.appcoda.com/core-data-preload-sqlite-database/

我发现 article/tutorial 的开头和结尾对您要执行的操作很有用。

我最近将数据预加载到测验的应用程序中,使用单独的 Xcode 模拟器创建所需的 3 个 SQLite 文件。这篇 文章将向您展示如何找到它们如何将它们"bundle" 到您想要的 Xcode 项目中'Pre-Load' 并会告诉您要在 'AppDelegate.swift' 文件中添加的代码以及添加位置。

3 件事可以帮助您在完成项目时避免问题...

  1. 确保您在模拟器(单独项目)中创建的数据的实体和属性名称(在您的数据模型(核心数据)中)是与您要 'PreLoad' 的项目中的相同。 [否则预加载不会工作]

  2. 要在“AppDelegate.swift”中添加的代码旨在指导您的应用程序去哪里获取预加载数据(请参阅随附的教程),但是,我发现我需要修改代码才能使其正常工作...(如下所述)

  3. 没有从 'AppDelegate.swift' 文件中删除任何内容,只是添加了 3 SQLite 文件 的名称...例如

在'AppDelegate.swift'核心数据栈中...你会发现...

lazy var managedObjectModel: NSManagedObjectModel = {
        // The managed object model for the application. This property is not optional. It is a fatal error for the application not to be able to find and load its model.
        let modelURL = NSBundle.mainBundle().URLForResource("EoMQ", withExtension: "momd")!
        return NSManagedObjectModel(contentsOfURL: modelURL)!
    }()

不用管它,但是在它的正下方你需要有下面的代码...

// ---------------------------------------------------
        // Create the coordinator and store
        let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)
        let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite") // originally = "SingleViewCoreData.sqlite" - then changed to new name... from imported sqlite files
        // ---------------------------------------------------
        // ** Load the Already made DB from Simulator **
        if !NSFileManager.defaultManager().fileExistsAtPath(url.path!) {

            let sourceSqliteURLs = [NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite")!, NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite-wal")!, NSBundle.mainBundle().URLForResource("Q100cdCoreData", withExtension: "sqlite-shm")!]

            let destSqliteURLs = [self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite"), self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite-wal"), self.applicationDocumentsDirectory.URLByAppendingPathComponent("Q100cdCoreData.sqlite-shm")]

            for index in 0 ..< sourceSqliteURLs.count {
                do {
                    try NSFileManager.defaultManager().copyItemAtURL(sourceSqliteURLs[index], toURL: destSqliteURLs[index])
                } catch {
                    print(error)
                }
            }
        }   

我在模拟器中创建的 3 个文件名为 "Q100cdCoreData" 但具有三个不同的扩展名... (a) .sqlite (b) .wal (c) .shm

但你需要通过教程并了解过程。