1

I have some methods that I am calling from the appDelegate to sync changes with persistent storage and iCloud.

The methods and the appDelegate work fine, my app syncs changes fine; however when the app is mergingChanges and persistentStoreDidChange I am trying to refresh the view controller data and change the view controller title to syncing.

I have tried changing the UIViewController title text and it does not change when merging or persistentStoreWillChange, also when using the reloadData() method for the view controller collection the app crashes with an unexpected nil when unwrapping an optional value.

The thing is the project has many tableview & colectionview all within a UITabController so I really need a refresh the data of the view controller in the window not just one specific view. Does anybody know how to refresh the viewcontroller data from the appDelegate ?

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();
}
RileyDev
  • 2,950
  • 3
  • 26
  • 61

1 Answers1

1

For above functionality you can use NSNotification Fire that notification to multiple classes when you want to update .

Alok
  • 24,880
  • 6
  • 40
  • 67