ios - 解包后,CoreDataStack 中的 ManagedObjectContext 返回 nil

标签 ios xcode swift core-data nsmanagedobjectcontext

我正在努力牢牢掌握 Core Data 以及大部分内容,尤其是子/父上下文和获取结果 Controller 。但是我有点卡在 CoreDataStack 上。我试图在程序的其余部分调用属性“上下文”。我可以在我的第一个 View 中打印上下文,但它似乎并没有通过导航栏在整个应用程序中持续存在。

这是CoreDataStack.swift

//exablishes the directory to be used for the stack in SQLite fashion
private lazy var applicationDocumentsDirectory: NSURL = {
    let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
    return urls[urls.count-1]
}()

//establishes the managed context by which managed objects interact in reading and writing
lazy var context: NSManagedObjectContext = {
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = self.psc
    return managedObjectContext
}()

//establishes the coordinator that connects the managed context with the persistent store
private lazy var psc: NSPersistentStoreCoordinator = { let coordinator = NSPersistentStoreCoordinator(
    managedObjectModel: self.managedObjectModel)

    let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent(self.modelName)

    do {
        let options = [NSMigratePersistentStoresAutomaticallyOption : true]

        try coordinator.addPersistentStoreWithType( NSSQLiteStoreType, configuration: nil, URL: url, options: options) //establishes the actual persistent store
    } catch {
        print("Error adding persistent store.")
    }
    return coordinator
}()
//establishes the object model that interacts with the GUI object data model
private lazy var managedObjectModel: NSManagedObjectModel = {
    let modelURL = NSBundle.mainBundle().URLForResource(self.modelName, withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
}()

这是 AppDelegate.Swift 中的相关部分

    let navigationController = window!.rootViewController as! UINavigationController
    let listViewController = navigationController.topViewController as! ViewController
    listViewController.coreDataStack = coreDataStack

最佳答案

这并不能真正解决您的问题,但我提供了另一种解决方案。我没有发现您提供的代码有任何问题。了解您稍后如何尝试在其他 View Controller 中获取 CoreDataStack 以及您认为它不持久存在的原因会很有帮助。


我通常让我的 CoreDataStack 类只是静态方法和变量的集合。

lazy 替换为 static(所有静态变量自动成为惰性变量),用于 CoreDataStack 上的所有变量。然后,在您的应用中任何您想要使用您的上下文的地方:

let ctx = CoreDataStack.context;

关于ios - 解包后,CoreDataStack 中的 ManagedObjectContext 返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36803951/

相关文章:

ios - iOS 的 kAudioDevicePropertyBufferFrameSize 替换

ios - 具有相同子值的 firebase 中的分页

swift - CAGradientLayer 设置为 scenekit 背景 block 场景

ios - 如何将 SwiftUI View 放置在 UIKit (UIViewRepresentable) View 之上? (错误或错误?)

ios - 带有多列图像的CoreText?

ios - Xcode 9 服务器 exportArchive : exportOptionsPlist error for key 'iCloudContainerEnvironment' : expected one of {Development, 生产}

c++ - 未知类型名称 'class' ;你是说 'Class' 吗?奥里奥触控

ios - 无法在 TableView 中使用自动布局约束

ios - 使用登录系统进行 Xcode UI 测试

ios - 存储在“字符串”中的对象的潜在泄漏