核心数据和iCloud

时间:2016-03-14 20:07:26

标签: swift core-data icloud

最近我一直在努力设置核心iCloud同步,并在我的应用程序中设置了核心数据。

我试图利用一些有趣的链接,如:

https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html#//apple_ref/doc/uid/TP40013491-CH3-SW8

https://www.objc.io/issues/10-syncing-data/icloud-core-data/

但没有真正的结果。

我创建了一个帮助类: `进口基金会 导入CoreData

类CloudHelper {

// Singleton
static let sharedInstance = CloudHelper()

let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

// MARK: Public functions    
// MARK: Notification Observers
func registerForiCloudNotifications(){
    let notificationCenter = NSNotificationCenter.defaultCenter()

    notificationCenter.addObserver(self, selector: Selector("storesWillChange:"), name: NSPersistentStoreCoordinatorStoresWillChangeNotification, object: appDelegate.persistentStoreCoordinator)
    notificationCenter.addObserver(self, selector: Selector("storesDidChange:"), name: NSPersistentStoreCoordinatorStoresDidChangeNotification, object: appDelegate.persistentStoreCoordinator)
    notificationCenter.addObserver(self, selector: Selector("persistentStoreDidImportUbiquitousContentChanges"), name: NSPersistentStoreDidImportUbiquitousContentChangesNotification, object: appDelegate.persistentStoreCoordinator)
}

// MARK: iCloud Support

private func iCloudPersistentStoreOptions() -> [String : String]{
    return [NSPersistentStoreUbiquitousContentNameKey: "myAppStore"]
}

private func persistentStoreDidImportUbiquitousContentChanges(changeNotification: NSNotification){
    let context = appDelegate.managedObjectContext

    context?.performBlock({ () -> Void in
        context?.mergeChangesFromContextDidSaveNotification(changeNotification)
    })
}

private func storesWillChange(notification: NSNotification){
    let context = appDelegate.managedObjectContext!

    context.performBlockAndWait({ () -> Void in
        let error: NSError
        if  context.hasChanges{
            do {
                try context.save()
            } catch let error1 as NSError {
                error = error1
                print("Error: \(error.localizedDescription)")
            }
        }

        context.reset()
    })

    // Refresh UI
}

private func storesDidChange(notification: NSNotification){
    // Refresh UI
}

} `

这是吗?因为当我运行应用程序并对核心数据进行一些更改时,即使我重新运行应用程序,我也看不到其他设备上的更改....我忘记了什么吗?

0 个答案:

没有答案