NSUbiquitousKeyValueStore sync issues

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-03 07:59:25

iCloud key-value store is not intended for use with data which requires regular frequent synchronization and it appears Apple does not guarantee the timeliness of these operations. Apple states:

The key-value store is intended for storing data that changes infrequently. If the apps on a device make frequent changes to the key-value store, the system may defer the synchronization of some changes in order to minimize the number of round trips to the server. The more frequently apps make changes, the more likely it is that later changes will be deferred and not show up on other devices right away.

If it is okay that your app does not synchronize immediately and your interest in this is to speed testing/debugging then it appears this will be a necessary frustration unless you change your technique.

The documentation for NSUbiquitousKeyValueStore states how synchronize works in detail. I think the assumption from Roger is that when you call:

[[NSUbiquitousKeyValueStore defaultStore] synchronize];

that it will upload the data straight away (assuming a network connection). This is NOT the case. Calling synchronize simply takes the keys and values you have set in memory and persists them to the local on-disk cache. Once this is done, the system then notifies iCloud that there is data ready to be uploaded. The key piece of information is mentioned below the synchronize method documentation:

This method does not force new keys and values to be written to iCloud. Rather, it lets iCloud know that new keys and values are available to be uploaded. Do not rely on your keys and values being available on other devices immediately. The system controls when those keys and values are uploaded. The frequency of upload requests for key-value storage is limited to several per minute.

Link to documentation

In short, the system controls when your keys and values are uploaded to iCloud and calling synchronize simply stores a local cache of them and notifies iCloud of new data waiting to be uploaded.

It might be a stale cache in the device's list of installed applications. Try deleting the app on the device and re-installing it. Or maybe reboot the device.

One sure thing is that you should file a report to bugreport.apple.com

Check your device console [via the organizer in Xcode]. It probably contains helpful information. Your entitlements may not be correct, even if the empty defaultStore sync returns YES.

In that case if you use jailbroken device you must delete AppSync before icloud and GameCenter support will work properly. Obviously I assume that you do have Apple dev account.

Dave Norfleet

I use this at launch and get immediate iCloud values or updateKVStoreItems: gets called in < 5 seconds.

- (void) initializeiCloud
{
    NSUbiquitousKeyValueStore* store = [NSUbiquitousKeyValueStore defaultStore];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(updateKVStoreItems:) name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification object:store];
    if ([store boolForKey:@"fakeSyncBit"])
    {
        NSLog(@"in initiCloud setting syncbit NO");
        [store setBool:NO forKey:@"fakeSyncBit"];  
    }
    else
    {
        NSLog(@"in initiCloud setting syncbit YES");
        [store setBool:YES forKey:@"fakeSyncBit"];
    }
    [store synchronize];
    NSLog(@"icloud store is synchronized. updateKVStoreItems: should be called shortly"); 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!