【发布时间】:2013-07-04 04:09:56
【问题描述】:
我有一个将文件存储在 iCloud 中的 iOS 应用程序。当我启动应用程序时,我想确定以前的设备是否已经上传了任何文件。我启动第一台设备并将文件添加到 iCloud(我可以在我的 Mac 上的 Mobile Documents 文件夹中看到它们)。然后我在第二台设备上启动应用程序并尝试使用以下 NSMetadataQuery 来查看是否已上传任何文件,但它返回 0 结果。如果我继续运行该查询,大约 8-10 秒后它会返回结果。
iCloudQuery = [[NSMetadataQuery alloc] init];
iCloudQuery.searchScopes = @[NSMetadataQueryUbiquitousDataScope];
NSString *filePattern = [NSString stringWithFormat:@"*.%@", @"txt"];
iCloudQuery.predicate = [NSPredicate predicateWithFormat:@"%K LIKE %@", NSMetadataItemFSNameKey, filePattern];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(iCloudQueryDidFinishGathering:) name:NSMetadataQueryDidFinishGatheringNotification object:iCloudQuery];
[iCloudQuery startQuery];
当我收到查询的通知时,resultCount 为 0
- (void)iCloudQueryDidFinishGathering:(NSNotification *)notification
{
NSMetadataQuery *query = [notification object];
[query disableUpdates];
[query stopQuery];
NSLog(@"Found %d results from metadata query", query.resultCount);
}
如果文件存在于 iCloud 中,NSMetadataQuery 是否应该返回一个 resultCount,即使它没有被下载?除了在 15-30 秒后尝试结束和超时之外,还有什么方法可以测试文件是否存在?
【问题讨论】:
-
您在 iCloud 中是否有指向该文件的有效 URL?
-
@SrikarAppal 我有一个指向 iCloud 容器的有效 URL,可以创建特定文件的路径,但它可能还不存在于本地 iCloud 容器中。
标签: ios objective-c icloud nsmetadataquery