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

醉酒当歌 提交于 2019-12-11 06:30:02

问题


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?


回答1:


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;


来源:https://stackoverflow.com/questions/11035276/why-isnt-startdownloadingubiquitousitematurlfileurl-starting-a-download-of-th

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!