【发布时间】:2015-11-05 08:59:03
【问题描述】:
我有一些从 appDelegate 调用的方法,用于将更改与持久存储和 iCloud 同步。
方法和 appDelegate 工作正常,我的应用同步更改正常; 然而当应用程序为mergingChanges 和persistentStoreDidChange 时,我正在尝试刷新view controller 数据并将view controller title 更改为同步。
我已尝试更改 UIViewController 标题文本,但在合并或 persistentStoreWillChange 时它不会更改,同样当对视图控制器集合使用 reloadData() 方法时,应用程序在展开可选值时以意外 nil 崩溃.
问题是该项目在UITabController 中有许多tableview 和colectionview,所以我真的需要在窗口中刷新view controller 的数据,而不仅仅是一个特定的视图。有人知道如何从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();
}
【问题讨论】:
标签: ios swift uiviewcontroller icloud appdelegate