Search code examples
swifticloudcloudkit

force sync with iCloud through a button


My app uses iCloud to sync. In my main view controller's viewWillAppear:

// Configure persistant store on iCloud

    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TodayViewController.persistentStoreDidChange), name: NSPersistentStoreCoordinatorStoresDidChangeNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TodayViewController.persistentStoreWillChange(_:)), name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: managedObjectContext.persistentStoreCoordinator)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TodayViewController.recieveICloudChanges(_:)), name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: managedObjectContext.persistentStoreCoordinator)

and viewWillDisappear:

override func viewWillDisappear(animated: Bool) {
    NSNotificationCenter.defaultCenter().removeObserver(self, name: NSPersistentStoreCoordinatorStoresDidChangeNotification, object: nil)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: managedObjectContext.persistentStoreCoordinator)
    NSNotificationCenter.defaultCenter().removeObserver(self, name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: managedObjectContext.persistentStoreCoordinator)
}

However, sometimes this takes too slow to sync. I would like the users to force sync by tapping a button, or maybe by swiping down the tableView to refresh (resync). How do I do that?

@IBAction forceSyncButton() {

** what to put in here?**

}

thank you!


Solution

  • You appear to be using Core Data's built-in iCloud support. Unfortunately there is no public API to force a sync to occur. It's handled automatically, and your app cannot do anything to change the schedule.