objective-c - 使用 For 循环时文档未上传到 iCloud - iOS

标签 objective-c ios for-loop icloud

我正在尝试将装满文档的本地文件夹上传到远程 iCloud 文件夹。我写这个方法来循环遍历本地文件夹中的文件数组,检查它们是否已经存在,如果不存在则将它们上传到 iCloud。注意-这段代码是在后台线程而不是主线程上执行的。

//Get the array of files in the local documents directory
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *localDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];

//Compare the arrays then upload documents not already existent in iCloud
for (int item = 0; item < [localDocuments count]; item++) {
    //If the file does not exist in iCloud, upload it
     if (![[iCloud previousQueryResults] containsObject:[localDocuments objectAtIndex:item]]) {
          NSLog(@"Uploading %@ to iCloud...", [localDocuments objectAtIndex:item]);
          //Move the file to iCloud
          NSURL *destinationURL = [[[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil] URLByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",[localDocuments objectAtIndex:item]]];
          NSError *error;
          NSURL *directoryURL = [[NSURL alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:[localDocuments objectAtIndex:item]]];
          BOOL success = [[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:directoryURL destinationURL:destinationURL error:&error];
          if (success == NO) {
              //Determine Error
              NSLog(@"%@",error);
          }
     } else {
          ...
     } }

当我运行此代码时,For 循环工作正常 - 我使用 NSLog 语句找出它正在上传的文件 - 并且 iCloud 中尚未存在的每个存储在本地的文件都应该启动上传。 For 循环完成后,我使用 developer.icloud.com 检查 iCloud 中现在有哪些文档.在本地存储的许多文件中,只有一个文件(我的应用程序一直在制作但从未使用过的 sqlite 文件)上传到 iCloud。 为什么使用for循环只能上传一个文件?

当我使用相同的代码上传单个文件(没有 For 循环)时,它们完美地上传到 iCloud。为什么 For 循环会阻碍文件的上传?这是否与 For Loop 继续执行下一行代码而不等待最后一个进程/行完成执行有关?这是怎么回事?

编辑:通常当我将大文件上传到 iCloud(不使用 For 循环)时,我几乎可以立即在 iCloud 开发站点上看到该文件处于待上传状态。我正在通过 WiFi 测试所有内容,我已经等了一段时间,但什么也没有出现(除了 sqlite 文件)。

编辑:我还使用不同的方法检查 iCloud 可用性,如果 iCloud 可用,则允许调用此方法。我还确保阅读了文档并观看了 Apple 网站上的 WWDC 视频,但是它们很复杂并且没有对我正在尝试做的事情提供太多解释。

编辑:修改上面的代码(添加错误功能)后,我现在在日志中收到此错误消息:

Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x1f5dd070 {NSUnderlyingError=0x208ad9b0 "The operation couldn’t be completed. (LibrarianErrorDomain error 2 - No source URL specified.)"}

这让事情变得更加困惑,因为其中一个文件上传成功,而其他文件则没有。

最佳答案

好的,最后的修改很有用。该错误提示缺少源 URL(代码中的 directoryURL)——可能是因为它是 nil。为什么会是 nil?因为你创建错了。您正在使用 -[NSURL initWithString:],并且文档说:

This string must conform to URL format as described in RFC 2396. This method parses URLString according to RFCs 1738 and 1808.

您传递的是文件路径,而不是 URL。如果您向 NSURL 传递它无法识别为有效 URL 的内容,它通常会返回 nil。看起来 NSURL 无论如何都会频繁生成文件 URL,但失败是这里的预期行为。据我所知,如果文件名不包含空格,它似乎可以工作,但这没有记录,也不是您可以依赖的东西。

您应该做的是更改初始化 directoryURL 的行,以使用接受文件路径的内容。像这样的东西:

NSURL *directoryURL = [NSURL fileURLWithPath:[documentsDirectory stringByAppendingPathComponent:[localDocuments objectAtIndex:item]]];

此外,确保验证 directoryURL 不是 nil,以防万一。

关于objective-c - 使用 For 循环时文档未上传到 iCloud - iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14185473/

相关文章:

ios - 出现键盘后UIWebView自动上移

iphone - 如何在 iPhone 中生成二维码?

javascript - stringByEvaluatingJavaScriptFromString 如何重入?

javascript - For循环找不到数组的最大值(Javascript)

android - For循环性能异常

iphone - MobileSafari 上页面标题的字体系列是什么?

objective-c - 在 dealloc 中使用 dispatch_async 安全吗?

ios - 刷新 token uber swift 返回 nil

iphone - 用于从 Photos.app 上传图像的移动 safari 的 html5 网络应用程序?

javascript - for...in 循环不遍历所有属性?