You are attempting to retrieve data from iCloud when it is not downloaded yet. The downloading operation takes some time and you should register for NSUbiquitousKeyValueStoreDidChangeExternallyNotification to be able to access fresh data from iCloud. Here is a sample code:
- (void) registerForiCloudNotificatons
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleChangesFromiCloud:)
name:NSUbiquitousKeyValueStoreDidChangeExternallyNotification
object:[NSUbiquitousKeyValueStore defaultStore]];
}
- (void) handleChangesFromiCloud: (NSNotification *) notification
{
NSDictionary * userInfo = [notification userInfo];
NSInteger reason = [[userInfo objectForKey:NSUbiquitousKeyValueStoreChangeReasonKey] integerValue];
// 4 reasons:
switch (reason) {
case NSUbiquitousKeyValueStoreServerChange:
// Updated values
break;
case NSUbiquitousKeyValueStoreInitialSyncChange:
// First launch
break;
case NSUbiquitousKeyValueStoreQuotaViolationChange:
// No free space
break;
case NSUbiquitousKeyValueStoreAccountChange:
// iCloud accound changed
break;
default:
break;
}
NSArray * keys = [userInfo objectForKey:NSUbiquitousKeyValueStoreChangedKeysKey];
for (NSString * key in keys)
{
NSLog(@"Value for key %@ changed", key);
}
}