如何通过今日扩展(iOS8)访问核心数据/CouldKit

如何通过今日扩展(iOS8)访问核心数据/CouldKit,ios,swift,core-data,cloudkit,today-extension,Ios,Swift,Core Data,Cloudkit,Today Extension,我有一个应用程序,它使用AppDelegate中标准Apple Core数据堆栈的核心数据。我修改了堆栈,以便使用CloudKit增强核心数据。所有数据在所有设备上都能很好地同步。到目前为止还不错 我想在我的应用程序中添加“今日”扩展,但我不知道如何访问数据。我一直在读AppGroup的概念,但我也读到我不应该/不能使用它来访问来自Today扩展的数据。所以我想我应该使用CloudKit,因为我了解到NSManagedObject在上传到iCloud时会转换为CKRecords 如何通过iClo

我有一个应用程序,它使用AppDelegate中标准Apple Core数据堆栈的核心数据。我修改了堆栈,以便使用CloudKit增强核心数据。所有数据在所有设备上都能很好地同步。到目前为止还不错

我想在我的应用程序中添加“今日”扩展,但我不知道如何访问数据。我一直在读AppGroup的概念,但我也读到我不应该/不能使用它来访问来自Today扩展的数据。所以我想我应该使用CloudKit,因为我了解到NSManagedObject在上传到iCloud时会转换为CKRecords

如何通过iCloud访问核心数据使用CloudKit enhancement存储的数据? iCloud中的数据存储在哪里?

虽然所有数据都很好地同步,但当我在iCloud仪表板中检查时,似乎没有数据存在。我读的所有文章似乎都只是解决了我问题的一部分

以下是我在应用程序中使用的修改后的核心数据堆栈:

    // MARK: - Core Data stack

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("AppName", withExtension: "momd")!
    return NSManagedObjectModel(contentsOfURL: modelURL)!
    }()

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 documentDirectory = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).last as! NSURL

    let storeUrl = documentDirectory.URLByAppendingPathComponent("AppName.sqlite")

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

    let storeOptions = [NSPersistentStoreUbiquitousContentNameKey:"ItemsCloudStore"]

    let url = storeUrl

    if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: storeOptions, error: &error) == nil {
        coordinator = nil
        // Report any error we got.
        var dict = [String: AnyObject]()
        dict[NSLocalizedDescriptionKey] = "Failed to initialize the application's saved data"
        dict[NSLocalizedFailureReasonErrorKey] = failureReason
        dict[NSUnderlyingErrorKey] = error
        error = NSError(domain: "YOUR_ERROR_DOMAIN", code: 9999, userInfo: dict)
        // Replace this with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog("Unresolved error \(error), \(error!.userInfo)")
        abort()
    }

    return coordinator
    }()

lazy var managedObjectContext: NSManagedObjectContext? = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    if coordinator == nil {
        return nil
    }


            var managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.MainQueueConcurrencyType)

    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
    }()


// MARK: - Core Data Saving support
func saveContext () {
    if let moc = self.managedObjectContext {
        var error: NSError? = nil
        if (moc.hasChanges && !moc.save(&error)) {

            println("Unresolved error \(error), \(error!.userInfo)")
            abort()

        } else {

            println("Context was updated")
        }
    }
}

经过一些研究,这似乎是苹果尚未解决的问题。核心数据和iCloud不能与扩展一起工作。查看本文:


有点扫兴。希望他们至少在iOS9中修复它。

您尝试过从扩展访问数据吗?是的。但是,当我使用AppGroups以便可以从应用程序和扩展访问时,我正在失去iCloud的功能。我无法使用CloudKit和上面的代码访问扩展中的数据。现在我正在将所有核心数据移到一个框架中。我不确定这是否是我应该采取的方法,或者它是否有机会发挥作用。不幸的是,这个链接似乎不再起作用了。