我有一个应用程序,我通过iCloud同步我的核心数据。同步工作正常,但我在iCloud同步后不会重新加载我在其中显示数据的tableView(在名为'ResultsView'的视图中);我需要转到另一个视图并返回结果视图以查看更改。
这就是我处理核心数据同步的方法(在AppDelegate.swift中):
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 url = self.applicationDocumentsDirectory.URLByAppendingPathComponent("MyApp.sqlite")
var error: NSError? = nil
var failureReason = "There was an error creating or loading the application's saved data."
// iCloud store
var storeOptions = [NSPersistentStoreUbiquitousContentNameKey : "MyAppStore",NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
// iCloud storeOptions need to be added to the if statement
do {
try coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: NSURL.fileURLWithPath(url.path!), options: storeOptions)
} catch var error1 as NSError {
error = error1
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()
} catch {
fatalError()
}
return coordinator
}()
同样在AppDelegate中,通过iCloud处理更新:
// MARK: - iCloud
// This handles the updates to the data via iCLoud updates
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)
}
我在ResultsView的viewWillAppear方法中获取了我的核心数据,以及viewWillAppear和viewDidAppear中的self.tableView.reloadData()
。
我缺少什么?
提前致谢
答案 0 :(得分:0)
[编辑 - 未正确阅读问题!]
如果您的数据可以在视图打开时更改(这可能是因为iCloud同步是异步的):您的视图需要知道它所基于的数据已经更改,因此在您的CoreData同步后调用{{3} } 在上面。另请参阅setNeedsDisplay。
如果您的数据在视图打开时无法更改,则在viewWillAppear中确保在您调用super之前数据已完全同步。请注意,与iCloud同步将是异步的,因此仅调用sync方法是不够的。如上所述,可以更容易地假设它将在视图打开时发生变化。