如何从 App Delegate 重新加载 UIViewController 数据

How to reload UIViewController data from App Delegate

我有一些方法是从 appDelegate 调用的,用于将更改与持久存储和 iCloud 同步。

方法和 appDelegate 工作正常,我的应用程序同步更改正常; 然而 当应用程序是 mergingChangespersistentStoreDidChange 我正在尝试刷新 view controller 数据并将 view controller title 更改为同步。

我已经尝试更改 UIViewController 标题文本,但在合并或 persistentStoreWillChange 时它并没有改变,在对视图控制器 collection 使用 reloadData() 方法时也是如此展开可选值时,应用程序崩溃并出现意外的 nil。

问题是该项目有很多 tableviewcolectionview 都在 UITabController 中,所以我真的需要刷新 [=] 中 view controller 的数据35=] 不只是一种特定的观点。有人知道如何从 appDelegate 刷新 viewcontroller 数据吗?

func mergeChanges(notification: NSNotification) {
    NSLog("mergeChanges notif:\(notification)")
    if let moc = managedObjectContext {
        moc.performBlock {
            moc.mergeChangesFromContextDidSaveNotification(notification)
            self.postRefetchDatabaseNotification()
        }
    }
    let vc = CollectionViewController()
    let view = self.window?.rootViewController

    vc.title = "Syncing"
    view?.title = "Syncing"
}

func persistentStoreDidImportUbiquitousContentChanges(notification: NSNotification) {
    self.mergeChanges(notification);
}

func storesWillChange(notification: NSNotification) {
    NSLog("storesWillChange notif:\(notification)");
    if let moc = self.managedObjectContext {
        moc.performBlockAndWait {
            var error: NSError? = nil;
            if moc.hasChanges && !moc.save(&error) {
                NSLog("Save error: \(error)");
            } else {
                // drop any managed objects
            }

            moc.reset();
        }

       let vc = CollectionViewController()
       vc.title = "Syncing"

        // reset UI to be prepared for a totally different
        // don't load any new data yet.
    }
}

func storesDidChange(notification: NSNotification) {
    // here is when you can refresh your UI and
    // load new data from the new store
    let vc = CollectionViewController()
    // vc.collectionView.reloadData()

    NSLog("storesDidChange posting notif");
    self.postRefetchDatabaseNotification();
}

对于上述功能,您可以使用 NSNotification 当您想要更新时向多个 类 发送该通知。