我正在尝试将文件从iCloud容器复制到应用程序的缓存目录。我一直在日志中获取The operation couldn’t be completed. (Cocoa error 512.)
,这是一个非常广泛的错误,意味着NSFileWriteUnknownError
,或者写入失败并且iOS不知道为什么。
这是NSMetadataQuery
完成并在iCloud中具有文件名时的代码。我将在下面简要描述代码。
- (void)loadData:(NSMetadataQuery *)query {
NSArray *cache = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[cacheDirectory stringByAppendingString:@"/issues"] error:NULL];
for (NSMetadataItem *item in [query results]) {
NSURL *name = [item valueForAttribute:NSMetadataItemURLKey];
NSString *nameString = [item valueForAttribute:NSMetadataItemPathKey];
NSError *error;
NSURL *cacheURL = [NSURL fileURLWithPath:cacheDirectory];
NSURL *destination = [cacheURL URLByAppendingPathComponent:[NSString stringWithFormat:@"issues/%@", [[nameString stringByDeletingLastPathComponent] lastPathComponent]] isDirectory:YES];
[[NSFileManager defaultManager] copyItemAtURL:[name URLByDeletingLastPathComponent] toURL:destination error:&error];
}
}
}
此应用程序同步报纸问题。在iCloud文档容器中,存在带有数据的文件夹(一个文件夹可以是
5.1.12
),并且在每个文件夹内是一个具有相同名称(5.1.12.pdf
)的PDF文件。知道为什么会这样吗?谢谢你的帮助。
iCloud: file://localhost/private/var/mobile/Library/Mobile%20Documents/AppBundleID/Documents/5.3.12/
Local: file://localhost/var/mobile/Applications/F91E115B-ECED-403E-AB61-41F7A99EF928/Library/Caches/issues/5.3.12/
最佳答案
当iCloud将文件同步到iOS设备时,它将首先在应用程序的容器中创建一个空的无数据文件,以告知您有可用的文件。这是您的NSMetadataQuery
报告的内容。
如果您查看NSMetadataItem
的其他键,您可能会注意到它说您的商品尚未下载。
在能够复制或打开该文件之前,您必须要求iCloud下载它。在项目路径上进行协调读取(就像您对任何打开或复制操作所做的那样)将隐式下载文件。
关于iphone - iCloud cocoa 错误512,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11588965/