繁体   English   中英

使用iOS应用传送核心数据

[英]Shipping core data with iOS app

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

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

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

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

这可能是一个冗长的答案。 最好将其作为教程进行解释。 以下链接将告诉您该怎么做。

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

我发现本文/教程的开头和结尾对您尝试执行的操作很有用。

我最近使用一个单独的Xcode模拟器创建了所需的3个SQLite文件,将数据预加载到了Quiz应用程序中。 本文将向您展示如何找到它们如何将 它们 “捆绑”到要“预加载”的Xcode项目中,并告诉您将代码添加到“ AppDelegate.swift”文件中的位置。

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

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

  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

但是您需要阅读本教程并了解该过程。

  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文件的路径(通常在应用程序目录中)。

因此,如果该文件不存在,它将从捆绑软件复制到应用程序目录,否则将失败。

暂无
暂无

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

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