gpt4 book ai didi

core-data - Swift - 用 iCloud Core Data 替换 Core Data

转载 作者:可可西里 更新时间:2023-11-01 00:38:01 26 4
gpt4 key购买 nike

如何用 iCloud Core Data 替换我现有的 Core Data?这是我的持久商店协调员:

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)

var notificacionCenter: NSNotificationCenter = NSNotificationCenter.defaultCenter()
notificacionCenter.addObserver(self, selector: "storesWillChange:", name:NSPersistentStoreCoordinatorStoresWillChangeNotification, object: coordinator)
notificacionCenter.addObserver(self, selector: "storesDidChange:", name:NSPersistentStoreCoordinatorStoresDidChangeNotification, object: coordinator)
notificacionCenter.addObserver(self, selector: "persistentStoreDidImportUbiquitousContentChanges:", name:NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: coordinator)

let url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyApp.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
var options: NSDictionary = [NSMigratePersistentStoresAutomaticallyOption : NSNumber(bool: true), NSInferMappingModelAutomaticallyOption : NSNumber(bool: true),
NSPersistentStoreUbiquitousContentNameKey : "iCloudStore"]
if coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: options, error: &error) == nil {
coordinator = nil
// Report any error we got.
let dict = NSMutableDictionary()
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
}()

我添加了通知,然后呢?如何更换?

最佳答案

您无需执行任何操作即可让 iCloud 存储的数据替换本地存储的数据。也就是说,本地核心数据会使用 iCloud 下载的数据进行更新,并在您背后保持一致。

但是,如果您在 UI 中显示了数据,那么当“新 iCloud 数据”到达时,“旧本地数据”可能仍会显示。 Apple 文档位于 Using the SQLite Store with iCould描述如何通过通知处理更新。总结该引用:在 NSPersistentStoreCoordinatorStoresWillChangeNotification 的处理程序中,如果存在任何未决更改,则保存它们,否则重置托管对象上下文并禁用用户界面。然后在 NSPersistentStoreCoordinatorStoresDidChangeNotification 处理程序中更新您显示的(和其他)数据并重新启用您的 UI。

这就是我在 iCloud 上使用的 Core Data。 storeOptions 是关键点。对于您的 Xcode 目标,您需要启用 iCloud 功能并确保您已选择“iCloud 文档”。

{
// ...

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)

var storeURL =
self.applicationDocumentsDirectory
.URLByAppendingPathComponent("iAppSwift.sqlite")

var storeOptions =
[NSPersistentStoreUbiquitousContentNameKey : "iAppSwiftStore"
// NSPersistentStoreRebuildFromUbiquitousContentOption: @(true)
]

//
self.registerCoordinatorForStoreNotifications (coordinator)

var error : NSError? = nil
var store : NSPersistentStore! =
coordinator.addPersistentStoreWithType (NSSQLiteStoreType,
configuration: nil,
URL: storeURL,
options: storeOptions,
error: &error)

if nil == store {
// handle error
}

return coordinator
}()

func registerCoordinatorForStoreNotifications (coordinator : NSPersistentStoreCoordinator) {
let nc : NSNotificationCenter = NSNotificationCenter.defaultCenter();

nc.addObserver(self, selector: "handleStoresWillChange:",
name: NSPersistentStoreCoordinatorStoresWillChangeNotification,
object: coordinator)

nc.addObserver(self, selector: "handleStoresDidChange:",
name: NSPersistentStoreCoordinatorStoresDidChangeNotification,
object: coordinator)

nc.addObserver(self, selector: "handleStoresWillRemove:",
name: NSPersistentStoreCoordinatorWillRemoveStoreNotification,
object: coordinator)

nc.addObserver(self, selector: "handleStoreChangedUbiquitousContent:",
name: NSPersistentStoreDidImportUbiquitousContentChangesNotification,
object: coordinator)
}

// ...
}

关于core-data - Swift - 用 iCloud Core Data 替换 Core Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27669173/

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