Swift:无法预加载Coredata [英] Swift: cannot preload Coredata

查看:110
本文介绍了Swift:无法预加载Coredata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在目标-构建阶段-复制捆绑资源下包含带有Objective-C的SQLite文件时,该文件将被完全复制到目标(即设备或模拟器)。在目标上,我得到了整个文件:表和内容(记录/行)。

When I include a SQLite file with Objective-C under "Target - Build Phases - Copy Bundle Resources", this file will be completely copied to the target, i.e. device or simulator. On the target, I get the whole file: tables and contents (Records/rows).

与Swift相同,表将被复制,但它们为空:无记录/行。 :-(

Doing the same with Swift, the tables will be copied, but they are empty: no records/rows. :-(

我还能做些其他事情吗?有什么把戏吗?

Can I do something additional? Is there any "trick"?

如何预载

我正在使用Xcode v6.1.1,与Beta 6.2相同。

I'm using Xcode v6.1.1, with Beta 6.2 it is the same.

推荐答案

这是我的解决方案(用于sqlite文件),我不知道为什么它在Swift中是如此奢侈,也许有一种更简单的方法?
许多到pbasdf的THX !!!

This is my solution (for sqlite-files). I don't know why it is so "extravagant" in Swift. Perhaps there's an easier way? Many THX to pbasdf!!!

重要:在* .sqlite文件下面您必须将* .sqlite-shm和* .sqlite-wal添加到您的项目
。此文件将自动添加到目标-构建阶段-复制捆绑资源中

Important: beneath the *.sqlite-file you must add the *.sqlite-shm and the *.sqlite-wal to your project This files will automatically be added to "Target - Build Phases - Copy Bundle Resources"

这基于 AppDelegate.swift的标准模板 ...

This is based on the Standard-Template for the "AppDelegate.swift"

lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it. This property is optional since there are legitimate error conditions that could cause the creation of the store to fail.
    // Create the coordinator and store
    var coordinator: NSPersistentStoreCoordinator? = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)

    let dataName = „MyData.sqlite" // must be replaced by the name of your file!
    let modelName = „MyData" // must be replaced by the name of your model!

    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(dataName)

    let fileManager = NSFileManager.defaultManager()
    let bundle = NSBundle.mainBundle()
    let fromURL = bundle.URLForResource(modelName, withExtension: "sqlite")

    // check sqlite-file: must it be installed?
    if !fileManager.fileExistsAtPath(url.path!) {
        self.copySqlliteFiles(modelName, databaseName: dataName)
    }

    var error: NSError? = nil
    var failureReason = "There was an error creating or loading the application's saved data."

    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options:  nil, error: &error) == nil {

...}

   return coordinator
}()

// MARK: - copy sqllite-files (c) by Ray Wenderlich & Team in „"Core Data by Tutorials"

// check sqlite-files: they must be installed...
func copySqlliteFiles(modelName: String, databaseName: String)
{
    let bundle = NSBundle.mainBundle()

    let baseDatabaseURL = bundle.URLForResource(modelName, withExtension: "sqlite")
    let documentsURL = self.applicationDocumentsDirectory
    let storeURL = documentsURL.URLByAppendingPathComponent(databaseName)
    NSFileManager.defaultManager().copyItemAtURL(baseDatabaseURL!, toURL: storeURL,error: nil)

    let baseSHMURL = bundle.URLForResource(modelName,withExtension: "sqlite-shm")
    let shmURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent(databaseName + "-shm")
    NSFileManager.defaultManager().copyItemAtURL(baseSHMURL!, toURL: shmURL, error: nil)

    let walURL = self.applicationDocumentsDirectory.URLByAppendingPathComponent(databaseName + "-wal")
    let baseWALURL = bundle.URLForResource(modelName, withExtension: "sqlite-wal")
    NSFileManager.defaultManager().copyItemAtURL(baseWALURL!, toURL: walURL, error: nil)
}

这篇关于Swift:无法预加载Coredata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆