【问题标题】:UITableView won't reload after iCloud sync (Swift 2)iCloud 同步后 UITableView 不会重新加载(Swift 2)
【发布时间】:2016-01-17 08:15:09
【问题描述】:

我有一个应用程序,我可以在其中通过 iCloud 同步我的核心数据。同步工作正常,但我在其中显示数据的 tableView(在名为“ResultsView”的视图中)在 iCloud 同步后不会重新加载;我需要转到另一个视图并返回我的 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()

我错过了什么?

提前致谢

【问题讨论】:

    标签: ios swift uitableview core-data icloud


    【解决方案1】:

    [编辑 - 没有正确阅读问题!]

    如果您的数据在视图打开时可以更改(这可能是因为 iCloud 同步是异步的):您的视图需要知道它所基于的数据已经更改,因此在您的 CoreData 同步后调用 setNeedsDisplay on它。另见this SO answer

    如果您的数据在视图打开时无法更改,请在 viewWillAppear 中确保在调用 super 之前完全同步数据。请注意,与 iCloud 同步将是异步的,因此仅调用同步方法是不够的。可能更容易假设它会在如上所述打开视图时发生变化。

    【讨论】:

    • 感谢您的回答。抱歉再次询问,但我应该在哪里打电话给setNeedsDisplay?我在viewDidLoad 和跟踪NSPersistentStoreDidImportUbiquitousContentChangesNotification 的函数中都试过,但都不起作用。我需要转到另一个视图并再次进入我的视图以查看更改。再次感谢
    • @CarlosBF 您在 viewDidLoad 中不应该需要它 - 系统将在视图首次出现时绘制视图。我不确定是否可以进一步帮助确定何时触发 setNeedsDisplay - CoreData 非常适合本地数据,但对于 iCloud 同步来说非常复杂,因此在经历了很多挫折之后,我转而使用 CloudKit。让“CoreData with iCloud”专家告诉您从哪个通知触发您的视图重绘可能值得一个单独的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-21
    相关资源
    最近更新 更多