通过按钮强制与iCloud同步

时间:2016-06-20 02:43:48

标签: swift icloud cloudkit

我的应用使用iCloud进行同步。在我的主视图中,控制器的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)

和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)
}

但是,有时这需要太慢才能同步。我希望用户通过点击按钮来强制同步,或者通过向下滑动tableView来刷新(重新同步)。我该怎么做?

@IBAction forceSyncButton(){

**这里放什么?**

}

谢谢你!

1 个答案:

答案 0 :(得分:3)

您似乎正在使用Core Data的内置iCloud支持。遗憾的是,没有强制同步发生的公共API。它会自动处理,您的应用无法更改计划。