1

我一直在努力将添加到我的项目中(这对我来说非常痛苦)并且我能够保存和删除文件,但我无法获得存储在 iCloud 中的文件列表。我尝试了大约 10 个不同网站(包括 Apple 文档)的解决方案。每当我打电话时[self.query startQuery];,一切似乎都在工作:正确的方法被调用,方法完全按照它们应该执行的方式执行。然后,当我在我的应用程序的 iCloud Documents 目录中请求文件的时,我得到两个括号,它们之间没有任何内容(当我在 NSLog 中查看时)File List: ( ):。我知道我的应用程序的 iCloud 文档目录中有许多不同形状、扩展名、大小和名称的不同文档,因为我一直在使用iCloud Developer网站检查事情是否正常。我设置查询的第一种方法如下:

- (void)syncWithCloud {
self.query = [[NSMetadataQuery alloc] init];
NSURL *mobileDocumentsDirectoryURL = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
[query setSearchScopes:[NSArray arrayWithObjects:NSMetadataQueryUbiquitousDocumentsScope, nil]];
//[query setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*'", NSMetadataItemFSNameKey]];
[query setPredicate:[NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%%K like \"%@*\"", [mobileDocumentsDirectoryURL path]], NSMetadataItemPathKey]];

//Pull a list of all the Documents in The Cloud
[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(processFiles:)
                                             name:NSMetadataQueryDidFinishGatheringNotification object:self.query];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processFiles:)
                                             name:NSMetadataQueryDidUpdateNotification object:self.query];

[self.query startQuery];
}

苹果提供的process files方法如下:

- (void)processFiles:(NSNotification*)aNotification {
    NSMutableArray *discoveredFiles = [NSMutableArray array];

    //Always disable updates while processing results.
    [query disableUpdates];

    //The query reports all files found, every time.
    NSArray *queryResults = [query results];
    for (NSMetadataItem *result in queryResults) {
        NSURL *fileURL = [result valueForAttribute:NSMetadataItemURLKey];
        NSNumber *aBool = nil;

        // Don't include hidden files.
        [fileURL getResourceValue:&aBool forKey:NSURLIsHiddenKey error:nil];
        if (aBool && ![aBool boolValue])
            [discoveredFiles addObject:fileURL];
    }

    //Update the list of documents.
    [FileList removeAllObjects];
    [FileList addObjectsFromArray:discoveredFiles];
    //[self.tableView reloadData];

    //Reenable query updates.
    [query enableUpdates];

    NSLog(@"File List: %@", FileList);
}

为什么这不给我一个文件列表或至少是某种数据?我是否错误地定义了 NSMetadata 查询,也许我的谓词格式不正确?我知道我做错了什么,因为 iCloud 不可能这么复杂(或者可能吗?)。
我在这里先向您的帮助表示感谢!

编辑#1:我将继续尝试不同的方法来解决这个问题。使用以下答案之一,我更改了谓词过滤器,如下所示:

[query setPredicate:[NSPredicate predicateWithFormat:@"NSMetadataItemFSNameKey LIKE '*'"]];

我还在通话添加了以下几行[query enableUpdates]

for (NSMetadataItem *item in query.results) {
        [FileList addObject:[item valueForAttribute:NSMetadataItemFSNameKey]];
 }

在该processFiles方法中,我尝试将所有代码放在后台线程上,但这没有什么区别——事实上,当代码不在后台线程上执行时,FileList我会得到 this(null)而不是 this ( )

我的问题可能与线程管理或内存分配有关吗?请注意,我使用的是 ARC。

编辑#2:该FileList变量是在 my 中定义的 NSMutableArray ,并在调用该方法之前@interface在该方法中初始化。-(id)initprocessFiles

编辑#3:当用断点测试我的代码时,我发现下面永远不会运行——甚至一次。这使我相信:
A. 正确的目录没有与
B. iCloud 看不到目录中的文件
C. 我的 NSMetadataQuery 没有正确设置 D. 完全不同的东西

这是启动永远不会运行的 for 循环的代码:

NSArray *queryResults = [query results];
for (NSMetadataItem *result in queryResults) {
4

2 回答 2

1

由于您已经正确设置了搜索范围,因此无需在谓词中使用特殊过滤器。只需使用:

query.predicate = [NSPredicate predicateWithFormat:@"NSMetadataItemFSNameKey == '*'"];

并获取数组使用:

NSMutableArray *array = [NSMutableArray array];
for (NSMetadataItem *item in query.results) {
        [array addObject:[item valueForAttribute:NSMetadataItemFSNameKey]];
}
于 2013-01-01T22:28:40.593 回答
0

我已经解决了我的问题。获取 iCloud 中的文件列表只是正确定义、分配和初始化属性的问题。SAE的回答和这个SO 发布帮助我解决了我的问题并创建了这个名为iCloud Document Sync的GitHub 存储库。iCloud Document Sync 类将整个 iCloud Document 存储过程简化为几行代码。此处链接的提交解决了我的问题中的问题。

于 2013-01-04T22:48:17.250 回答