gpt4 book ai didi

ios - 如何通过 Today Extension (iOS 8) 访问 Core Data/CloudKit

转载 作者:行者123 更新时间:2023-11-28 09:00:56 26 4
gpt4 key购买 nike

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

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

我如何通过 iCloud 访问由 Core Data 使用 CloudKit 增强功能存储的数据?这些数据究竟存储在 iCloud 中的什么位置?

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

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

    // 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")
}
}
}

最佳答案

经过一些研究,这似乎是 Apple 尚未解决的问题。 Core Data 和 iCloud 不能与 Extensions 一起使用。看看这篇文章:

https://devforums.apple.com/message/1050891#1050891

有点无赖。希望他们至少在 iOS9 中修复它。

关于ios - 如何通过 Today Extension (iOS 8) 访问 Core Data/CloudKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32039465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com