Simon
Simon

Reputation: 25983

Why isn't startDownloadingUbiquitousItemAtURL:fileURL: starting a download of the item?

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?

Upvotes: 1

Views: 3091

Answers (1)

voromax
voromax

Reputation: 3389

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;

Upvotes: 2

Related Questions