iCloud:NSFileManager的startDownloadingUbiquitousItemAtURL的回调?

use*_*234 14 ios ios5 icloud icloud-api

我使用NSFileManagerstartDownloadingUbiquitousItemAtURL从iCloud的本地副本(本地还没有文件在这种情况下副本)下载文件.我似乎找不到这个过程的回调.我需要回调告诉我的应用程序所请求的文件已完成下载(到本地副本),因此其他任务可以开始读取文件的内容并执行操作.

我可以检查文件是否已下载.但它会不断涉及民意调查.有没有办法在没有设置计时器的情况下执行此操作?

wby*_*ung 29

我相信这种方法可以与基于Apple文档的文件协调器一起使用.所以你需要使用像这样的文件协调器:

NSURL *itemURL = nil; // this is the URL you want to read from

__block NSData *data = nil;
NSError *error = nil;
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[coordinator coordinateReadingItemAtURL:itemURL options:0 error:&error byAccessor:^(NSURL *newURL) {
    data = [NSData dataWithContentsOfURL:newURL];
}];
Run Code Online (Sandbox Code Playgroud)

但是,这将是同步的,因此如果您想要异步执行某些操作,可以使用块,如下所示:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    // read info from the URL using the code above
    dispatch_async(dispatch_get_main_queue(), ^{
        // handle data read from the URL
    });
});
Run Code Online (Sandbox Code Playgroud)

  • Apple创建的API可以分为两类:1)那些吮吸,混淆为地狱,模糊,不完整和不良记录以及2)可通行的API.没有一个设计精良的API.别误会我的意思.我喜欢苹果公司的产品,但处理开发人员和API的Apple并不是人们所知道的高标准Apple.反之. (3认同)