1

I'm using an NSMetadataQuery to check for files in iCloud. Inside the NSMetadataQueryDidFinishGatheringNotification handler, if the item isn't downloaded or downloading I start downloading it using startDownloadingUbiquitousItemAtURL:fileURL: - but recently, after using the app on two devices, the documents seem to have got stuck; startDownloadingUbiquitousItemAtURL:fileURL: doesn't kick off a download any more.

Here's an abbreviated form of the code:

    for (NSMetadataItem * result in query.results)
    {   
        NSURL *fileURL = [result valueForAttribute:NSMetadataItemURLKey];

        // snipped: checks that the item isn't hidden or already downloaded

        if (![fileURL getResourceValue:&isDownloading forKey:NSURLUbiquitousItemIsDownloadingKey error:&error] || !isDownloading)
        {
            NSLog(@"Error %@ getting downloading state for item at %@", error, fileURL);
            continue;
        }

        if ([isDownloading boolValue])
        {
            if (![fileURL getResourceValue:&downloadPercent forKey:NSURLUbiquitousItemPercentDownloadedKey error:&error] || !downloadPercent)
            {
                NSLog(@"Error %@ getting downloaded progress for item at %@", error, fileURL);
                continue;
            }

            NSLog(@"Item at %@ has is %@%% downloaded", fileURL, downloadPercent);
        }
        else
        {
            NSLog(@"Starting to download item at %@", fileURL);

            if ([[NSFileManager defaultManager] startDownloadingUbiquitousItemAtURL:fileURL error:&error])
            {
                isDownloading = nil;

                if ([fileURL getResourceValue:&isDownloading forKey:NSURLUbiquitousItemIsDownloadingKey error:&error] && isDownloading)
                {
                    if ([isDownloading boolValue])
                    {
                        NSLog(@"After starting to download, item at %@ is downloading.", fileURL);
                    }
                    else
                    {
                        NSLog(@"After starting to download, item at %@ is still not downloading!", fileURL);
                    }
                }
                else
                {
                    NSLog(@"Error %@ getting downloading state again for item at %@", error, fileURL);
                }
            }
            else
            {
                NSLog(@"Error %@ starting to download item at %@", error, fileURL);
            }
        }
    }

and what I'm seeing is:

Starting to download item at XXXXXXXX
After starting to download, item at XXXXXXXX is still not downloading!
Starting to download item at YYYYYYYY
After starting to download, item at YYYYYYYY is still not downloading!
Starting to download item at ZZZZZZZZ
After starting to download, item at ZZZZZZZZ is still not downloading!

Why isn't startDownloadingUbiquitousItemAtURL:fileURL: starting a download of the item? If it's due to some conflict, how do I detect that conflict?


UPDATE: I've had a look at the device console, and I see this:

MyApp[NNNN] <Warning>: Starting to download item at XXXXXXXX
librariand[MMMM] <Error>: unable to download XXXXXXXX (0x8000000000000000)
MyApp[NNNN] <Warning>After starting to download, item at XXXXXXXX is still not downloading!
MyApp[NNNN] <Warning>Starting to download item at YYYYYYYY
librariand[MMMM] <Error>: unable to download YYYYYYYY (0x8000000000000000)
MyApp[NNNN] <Warning>After starting to download, item at YYYYYYYY is still not downloading!
MyApp[NNNN] <Warning>Starting to download item at ZZZZZZZZ
librariand[MMMM] <Error>: unable to download ZZZZZZZZ (0x8000000000000000)
MyApp[NNNN] <Warning>After starting to download, item at ZZZZZZZZ is still not downloading!

So it looks like at least the daemon is being told to download the file, even though it can't. Is there any documentation on librariand errors?

Simon
  • 25,468
  • 44
  • 152
  • 266
  • I've seen this too. icloud takes its sweet time.. a event or notification when download is complete would be nice as well so we could not have to loop thru this every few seconds to see whats going on. – J3RM Jul 06 '12 at 16:16
  • Solved with this : [http://stackoverflow.com/questions/9675303/icloud-callback-for-nsfilemanagers-startdownloadingubiquitousitematurl][1] [1]: http://stackoverflow.com/questions/9675303/icloud-callback-for-nsfilemanagers-startdownloadingubiquitousitematurl – RelativeGames Dec 02 '12 at 23:16

1 Answers1

2

Take a closer look at NSMetadataItem in your NSMetadataQuery results. When you iterating through the results of your query request you can get values for the following attributes instead:

NSString * const NSMetadataUbiquitousItemIsDownloadedKey;
NSString * const NSMetadataUbiquitousItemIsDownloadingKey;
NSString * const NSMetadataUbiquitousItemIsUploadedKey;
NSString * const NSMetadataUbiquitousItemIsUploadingKey;
NSString * const NSMetadataUbiquitousItemPercentDownloadedKey;
NSString * const NSMetadataUbiquitousItemPercentUploadedKey;
voromax
  • 3,369
  • 2
  • 30
  • 53
  • Yes, I know - if you look in the example code I use `NSURLUbiquitousItemIsDownloadingKey` to find whether the item is downloading. The problem is that event though I've called `startDownloadingUbiquitousItemAtURL:fileURL:` the item is still not downloading. – Simon Jul 19 '12 at 09:05
  • Again. There is a difference between `NSMetadataUbiquitousItemIsDownloadingKey ` attribute of `NSMetadataItem` object and `NSURLUbiquitousItemIsDownloadingKey` of `NSURL` object. The former should be using for iCloud metadata query handling. The same for the other attributes – voromax Jul 19 '12 at 09:38
  • I've modified my app to use valueForAttribute where possible; although there doesn't seem to be an equivalent of NSURLIsHiddenKey. I'll update here once I reproduce the stuck state. – Simon Jul 28 '12 at 11:38
  • I get a slightly different librariand error: "librariand[4220] : item update observer error: Connection invalid" – Simon Jul 28 '12 at 11:42
  • @simon You can get rid of hidden elements during the metadata request construction. This is because it is a metadata information but not the actual filesystem items. Your second comment needs more data to get answered. – voromax Jul 28 '12 at 14:48