ICloud does not work, the application launches for the first time

For my application, I use the iCloud key value store to store some user settings. It syncs perfectly between my iPad and iPhone when both have the app installed. My problem is that when I uninstall the application and I launch it fresh, it does not have any settings from iCloud in FIRST TIME, I launch it. After I started it again, although it did not have settings for the first time, it has them.

I used some NSLogs to see what it sees in the key-value container, and the first time it launches the application, it says β€œ(null)”, but on subsequent launches it returns the NSArray that was saved earlier.

I gladly provided the code, but I'm not quite sure what is relevant here.

I would be grateful for any help, this problem is driving me crazy ...

+5
source share
2 answers

Add an observer for NSUbiquitousKeyValueStoreDidChangeExternallyNotificationand sync NSUbiquitousKeyValueStore. Wait for the call to be called soon.

if([[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil])
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyValueStoreChanged:)
                                                 name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
                                               object:[NSUbiquitousKeyValueStore defaultStore]];

    [[NSUbiquitousKeyValueStore defaultStore] synchronize];
}
else
{
        NSLog(@"iCloud is not enabled");
}

And then use NSUbiquitousKeyValueStoreChangeReasonKeyto distinguish between first time synchronization and server synchronization.

-(void)keyValueStoreChanged:(NSNotification*)notification 
{
    NSLog(@"keyValueStoreChanged");

    NSNumber *reason = [[notification userInfo] objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey];

    if (reason) 
    {
        NSInteger reasonValue = [reason integerValue];
        NSLog(@"keyValueStoreChanged with reason %d", reasonValue);

        if (reasonValue == NSUbiquitousKeyValueStoreInitialSyncChange)
        {
            NSLog(@"Initial sync");
        }
        else if (reasonValue == NSUbiquitousKeyValueStoreServerChange)
        {
            NSLog(@"Server change sync");
        }
        else
        {
            NSLog(@"Another reason");
        }
    }
}
+6
source

There may be a delay between your application (and the running one) and the loading of KVS initial values. If you correctly registered in the change notification, you should see the values ​​included.

Your code should always look like this, usually in the delegate method -applicationDidFinishLaunching::

_store = [[NSUbiquitousKeyValueStore defaultStore] retain]; // this is the store we will be using
// watch for any change of the store
[[NSNotificationCenter defaultCenter] addObserver:self
      selector:@selector(updateKVStoreItems:)
      name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
      object:_store];
// make sure to deliver any change that might have happened while the app was not launched now
[_store synchronize];
+1
source

All Articles