ios – 核心数据iCloud Sync确实更改了商店通知不刷新UI

前端之家收集整理的这篇文章主要介绍了ios – 核心数据iCloud Sync确实更改了商店通知不刷新UI前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法处理这个问题,我正在尝试将iCloud和Core Data集成到我的应用程序中,并且我坚持使用iCloud同步部分.

我的完整场景:

>本地核心数据存储以初始数据为种子
>稍后,应用程序会询问用户有关iCloud或本地数据存储的信息
>如果用户选择iCloud,则当前本地存储将迁移到iCloud存储
>迁移后,将使用iCloud Store重新加载上下文和持久性存储协调器
>从新上下文中重新获取数据(此处为麻烦)

如果我们拿走有关迁移的讨论并专注于使用iCloud加载持久性商店协调器,我认为问题与此有关
NSPersistentStoreCoordinatorStoresDidChangeNotification事件.

我只是不明白.在我阅读的每篇文章中,我都看到了类似的内容:“在这里重新加载UI”.但它对我不起作用.

重新加载协调员功能

func configureContext() -> NSManagedObjectContext? {
    // Create the coordinator and context
    let coordinator = NSPersistentStoreCoordinator(managedObjectModel: self.managedObjectModel)

    //remove all observers from "prevIoUs" coordinator
    deregisterForStoreChanges()
    //and register them for new one
    registerObservers(persistentStoreCoordinator: coordinator)

    do {

        //storeSettings.url = applicationDocumentsDirectory.URLByAppendingPathComponent("iCloud.sqlite")
        //storeSettings.options = [NSPersistentStoreUbiquitousContentNameKey:"iCloudProject"]

        try coordinator.addPersistentStoreWithType(NSsqliteStoreType,configuration: nil,URL: storeSettings.url,options: storeSettings.options)

    } catch {
        //Here was standard error handler I didn't changed it,but removed for listing
        abort()
    }
    persistentStoreCoordinator = coordinator

    let managedObjectContext = NSManagedObjectContext(concurrencyType: .MainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator

    //not quite sure about the next string,didn't test it yet
    managedObjectContext.mergePolicy = NSMergePolicy(mergeType:NSMergePolicyType.MergeByPropertyStoreTrumpMergePolicyType )

    //tried to refetch my records here but,with no luck.
    //refetchUIRecords()
    return managedObjectContext
}

还有我的NSPersistentStoreCoordinatorStoresDidChangeNotification事件函数

func persistentStoreCoordinatorDidChangeStores(notification:NSNotification){

    if notification.userInfo?[NSAddedPersistentStoresKey] != nil {
        print("-----------------------")
        print("ADDED")
        print("-----------------------")
        //deduplicateRecords()

        //if iCloud -> refetch UI
        if NSUserDefaults.standardUserDefaults().boolForKey("iCloud"){
            self.createTimer()
        }
    }
    print("Did Change")

}

创建计时器功能只是在实际重新获取和刷新UI之前等待1秒的功能.

问题

当我们从我的场景到达第4步,并调用configureContext函数时,
我在控制台中看到了这个:

2016-04-12 13:31:27.749 TestingiCloudProject[2052:1142902] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](898): CoreData: Ubiquity:  mobile~567404C0-9D84-4C07-A0F8-D25832CB65D8:iCloudProject
Using local storage: 1 for new NSFileManager current token <ef7a917f bca47ecf 5d58862d cbe9998d 7e53e5ea>
Did Change
Did Change
-----------------------
ADDED
-----------------------
Timer
Did Change
Refetch
Refetching...
Number of records after fetch: 1 //must be more than 1 
2016-04-12 13:31:30.090 TestingiCloudProject[2052:1143055] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](898): CoreData: Ubiquity:  mobile~567404C0-9D84-4C07-A0F8-D25832CB65D8:iCloudProject
Using local storage: 0 for new NSFileManager current token <ef7a917f bca47ecf 5d58862d cbe9998d 7e53e5ea>

正如你可以看到我的获取请求在使用本地存储之前执行:0这就是为什么我从本地存储(1条记录)接收记录而不是iCloud(包含多于1条)的记录.

我不明白何时重新获取我的记录.对于想要了解更多有关Core Data和iCloud的所有人,我从great source获取计时器部分.
但是,1秒钟没有投入,2秒是我想要的工作,但是如果iCloud商店比我的更大,或者网络连接会比我的更糟糕,我不认为计时器就是这样.

我希望有人已经面临这个微不足道的问题.

编辑

我没有从Apple dev论坛找到任何帮助,所以我激活了我的代码技术支持令牌,我希望他们可以帮助我.一旦我解决了我的问题,我就会写一个答案.但是,如果您读过这个问题的人知道可能的答案,请立即发布.

解决方法

每当从无处不在的内容存储库导入数据时,持久性存储协调器会触发另一个通知NSPersistentStoreDidImportUbiquitousContentChangesNotification.您可以在此处将更改与NSManagedObjectContext合并.

When the ubiquity container receives changes from iCloud,Core Data posts an NSPersistentStoreDidImportUbiquitousContentChangesNotification notification. This notification’s userInfo dictionary is structured similarly to that of an NSManagedObjectContextDidSaveNotification notification except for that it contains NSManagedObjectID instances rather than NSManagedObject instances. Therefore you can merge in changes from other peers in the same way that you merge changes from other managed object contexts. Call mergeChangesFromContextDidSaveNotification: on your managed object context,passing in the notification object posted by Core Data.

https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html

猜你在找的iOS相关文章