gpt4 book ai didi

ios - 将 iCloud 文件复制到沙箱

转载 作者:行者123 更新时间:2023-11-28 18:03:24 25 4
gpt4 key购买 nike

我正在寻找一种更好的方法来将一堆文件从我的 iCloud 容器下载到我的沙箱。这是我目前使用的:

for (FileAttachments *fileLink in items) {
NSURL *cloudFileURL = [fileLink urlForCloudAttachmentWithName];

NSURL *fileURL = [backupCloudLocalDirectoryURL URLByAppendingPathComponent: fileLink.fileName];

NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];

[fileCoordinator coordinateReadingItemAtURL:fileURL options:NSFileCoordinatorReadingWithoutChanges error:&error

byAccessor:^(NSURL *newURL) {

NSError *blockError = nil;

[fileManager copyItemAtURL:cloudFileURL toURL:fileURL error:&blockError];
}];
}
}

用这种方式复制 iCloud 项目有什么问题吗?在生产中,我有一些用户提示他们的所有文件都没有下载。改用 NSFileManager 的 startDownloadingUbiquitousItemAtURL:error 是否更好?如果是这样,为什么?谢谢。

最佳答案

从评论中我仍然不完全清楚您是如何发现 URL 的,但是一些影响您情况的重要细节是:

  1. 使用 coordinateReadingItemAtURL:block: 与从 iCloud 下载文件无关NSFileCoordinator 的目的是在文件的读取器和写入器之间进行协调,例如,您不会让两个线程同时尝试写入同一个文件。您将文件协调器与 iCloud 一起使用,因为 iCloud 系统需要读取和写入文件,您的应用程序也是如此。使用协调器可避免损坏文件,但同样与下载文件无关。

  2. 要从 iCloud 下载文件,您需要使用 startDownloadingUbiquitousItemAtURL:error: 然后等待文件下载。这里的正常流程是:

    一个。使用 NSMetadataQuery 查找 iCloud 帐户中存在的文件

    使用 startDownloadingUbiquitousItemAtURL:error: 确保它们在本地设备上可用。

    您需要使用此调用的原因很简单,因为这就是 iCloud 在 iOS 上的工作方式。在您要求文件下载之前,文件不会下载,这就是您的要求。 [在 OS X 上不同,所有内容都会自动下载。]

  3. 不能简单地从 iCloud URL 复制到另一个位置,除非您已经知道该文件已下载。文件复制操作不会下载文件,因此如果文件在本地不可用,复制将失败。

关于ios - 将 iCloud 文件复制到沙箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22954215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com