如何用iCloud核心数据取代我现有的核心数据? 这是我persistentStoreCoordinator:Swift - 用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 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合并?如何在其他设备上检测是否更改刷新UI? – 2014-12-29 14:09:35
'其他设备'将得到'WillChange'和'DidChange'通知。在处理这些通知的处理程序中,您将更新显示的(和其他)数据。 – GoZoner 2014-12-30 06:37:11
'var options:NSDictionary = [NSMigratePersistentStoresAutomaticallyOption:NSNumber(bool:true),NSInferMappingModelAutomaticallyOption:NSNumber(bool:true), NSPersistentStoreUbiquitousContentNameKey:“iCloudStore”iCloud已激活? – 2014-12-30 11:07:03