12

I sometimes have a problem with opening UIDocuments which are stored on iCloud. I call the following code at the very beginning of my app after checking if iCloud is available and getting the results of the NSMetaDataQuery. This works in 98% of all cases. It's just sometimes (e.g. sometimes after re-installing the app) that the completionHandler will never be called:

FRNBDocument *doc = [[FRNBDocument alloc] initWithFileURL:fileURL];
[doc openWithCompletionHandler:^(BOOL success) {

    NSLog(@"... doc: openedWithCompletionHandler");

As you can see from the code, I'm initialising a new UIDocument instance (FRNBDocument), so it can't be the case than an old UIDocument is still open and thus this one fails to open.

The only way to get the app working again is to kill it and then go to the settings.app > iCloud > Documents & Data and turn it OFF and then turn it ON again. When I go back to the app, the UIDocs will be loaded without a problem.

I've noticed that other apps (e.g. xTrail by sophiestication) have the same problem and that I can only get them running again after doing the trick described above.

What is going on?

n.evermind
  • 11,944
  • 19
  • 78
  • 122
  • Can you post the code you use to attach to iCloud and the rest of the file code..not enough code here. – deleted_user Sep 06 '12 at 07:35
  • @stackmonster I use the normal code which I'd be happy to post, but I don't think it will help much. Spoke to Apple and it appears as if its a well-known bug. – n.evermind Sep 08 '12 at 09:55

3 Answers3

3

OK, this seems to be an iCloud bug. I've been in touch with Apple and they suggested to file a bug report. And so I did (id 12260670).

What I'll do now is to simply implement a timer. If the UIDocs have not loaded after 20 secs, I'll show an AlertView, telling the user to go to the settings.app > iCloud > Documents & Data and turn it OFF and then turn it ON again.

This is so frustrating. Unbelievable.

n.evermind
  • 11,944
  • 19
  • 78
  • 122
  • 1
    @stackmonster If it's not a bug, please explain why. I'll be happy to mark you answer as the correct one if it solves the problem. Thanks. BTW I've tried to use a Technical Support Incidence for this Question, so I'm sure they would have explained the API to me if this wasn't a bug. But they refunded the TCI incidence and said I should file a bug instead. – n.evermind Sep 15 '12 at 15:37
1

I am having a similar problem. The NSMetadataQuery returns a result but the openWithCompletionHandler: very occasionally does not complete and so the app's data would not load. For a time I had a workaround by checking the NSMetadataUbiquitousItemIsDownloadedKey before attempting to load the document. If it wasn't downloaded I called evictUbiquitousItemAtURL and then startDownloadingUbiquitousItemAtURL. I then had to keep polling with an NSMetadataQuery for the NSMetadataUbiquitousItemIsDownloadedKey to be true at which time I could successfully call openWithCompletionHandler. This appeared to solve the problem. However frustratingly this appears to either no longer work in iOS 6 or doesn't work all the time anymore. Please keep us updated if you find a better workaround or get an update on the bug from Apple.

Dave Ross
  • 673
  • 4
  • 12
0

iCloud access requires returning calling of the function

- (NSURL *)URLForUbiquityContainerIdentifier:(NSString *)containerID

which returns

A URL pointing to the specified ubiquity container, or nil if the container could not be located or if iCloud storage is unavailable for the current user or device.

Reference

As the iCloud is not that responsive, I check if I get the URL and then only proceed to further syncing of the data.

NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq) 
{
    NSLog(@"iCloud access at %@", ubiq);
    [self loadDocument];
} 
else 
{
    NSLog(@"No iCloud access");
}
Vimal Venugopalan
  • 4,091
  • 3
  • 16
  • 25
  • I don't think this is the problem. I get a valid ubiq and I even get results via NSMetaDataQuery. However, none of the results can be loaded once iCloud is stuck. – n.evermind Sep 09 '12 at 15:06
  • you can add notification using [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(queryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:query]; – Vimal Venugopalan Sep 09 '12 at 15:29
  • I've done all that. I could post the entire code, but it would be quite tedious and beside the point. I've made sure that everything is fine- and it works 95% of the time. Sometimes, only sometimes, iCloud plays up and won't load an UIDocument. It's apparently a known issue- other devs had the same problem. – n.evermind Sep 09 '12 at 16:53