Swift 核心数据和iCloud
最近,我一直在尝试设置核心iCloud与我的应用程序中已设置的核心数据同步 我尝试使用一些有趣的链接,如: 及 但没有真正的结果 我创建了一个助手类: “进口基金会” 导入CoreData 类CloudHelper{Swift 核心数据和iCloud,swift,core-data,icloud,Swift,Core Data,Icloud,最近,我一直在尝试设置核心iCloud与我的应用程序中已设置的核心数据同步 我尝试使用一些有趣的链接,如: 及 但没有真正的结果 我创建了一个助手类: “进口基金会” 导入CoreData 类CloudHelper{ // Singleton static let sharedInstance = CloudHelper() let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate // MA
// Singleton
static let sharedInstance = CloudHelper()
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
// MARK: Public functions
// MARK: Notification Observers
func registerForiCloudNotifications(){
let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: Selector("storesWillChange:"), name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: appDelegate.persistentStoreCoordinator)
notificationCenter.addObserver(self, selector: Selector("storesDidChange:"), name: NSPersistentStoreCoordinatorStoresDidChangeNotification, object: appDelegate.persistentStoreCoordinator)
notificationCenter.addObserver(self, selector: Selector("persistentStoreDidImportUbiquitousContentChanges"), name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: appDelegate.persistentStoreCoordinator)
}
// MARK: iCloud Support
private func iCloudPersistentStoreOptions() -> [String : String]{
return [NSPersistentStoreUbiquitousContentNameKey: "myAppStore"]
}
private func persistentStoreDidImportUbiquitousContentChanges(changeNotification: NSNotification){
let context = appDelegate.managedObjectContext
context?.performBlock({ () -> Void in
context?.mergeChangesFromContextDidSaveNotification(changeNotification)
})
}
private func storesWillChange(notification: NSNotification){
let context = appDelegate.managedObjectContext!
context.performBlockAndWait({ () -> Void in
let error: NSError
if context.hasChanges{
do {
try context.save()
} catch let error1 as NSError {
error = error1
print("Error: \(error.localizedDescription)")
}
}
context.reset()
})
// Refresh UI
}
private func storesDidChange(notification: NSNotification){
// Refresh UI
}
}
`
是这个吗?因为当我运行应用程序时,它对核心数据进行了一些更改,即使我重新运行应用程序,我也看不到其他设备上的更改。。。。我忘了什么吗?因为
iCloudPersistentStoreOptions()
是私有的,您在何处将此–关键且必需的–选项传递给永久存储?实际上,我不知道在何处使用它…您必须在添加存储之前延迟创建变量persistentstorecordinator
时传递它。实际上,文档(您的第一个链接)中对它进行了很好的描述,我刚刚意识到我需要将它放在appDelegate persistentStoreCoordinator方法中。但就是这样吗?是的,就是这样。并确保通知可用