iCloud NSMetadata查询结果为空 [英] iCloud NSMetadata query results are blank

查看:146
本文介绍了iCloud NSMetadata查询结果为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been working on adding icloud to my project (which is quite a pain in the buns) and I'm able to save and remove files, but I can't get a list of the files stored in iCloud. I've tried solutions from about 10 different websites (including the Apple documentation). Whenever I call [self.query startQuery]; everything seems to be working: The correct methods get called, the methods execute exactly as they should. Then when I ask for an nsarray of the files in my app's iCloud Documents directory I get two parenthesis with nothing between them (when I view in NSLog): File List: ( ). I know for a fact that there are many different documents of all shapes, extensions, sizes, and names in my app's iCloud Documents directory because I've been using the iCloud Developer site to check if things are working. My first method to setup the query is as follows:

- (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];
}

进程文件方法是下一个:

- (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无法复杂(或者可能吗?)。

提前感谢您的帮助!

Why doesn't this give me a list of files or at least some kind of data? Am I defining the NSMetadata query wrong, maybe my predicate isn't formatted right? I know I'm doing something wrong because there's no way iCloud could be this complicated (or could it?).
Thanks for the help in advance!

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

Edit #1: I am continuing to try different approaches to this problem. Using one of the answers below I have changed the predicate filter as follows:

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

我还在 > [query enableUpdates] call:

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

processFiles 方法,我已经尝试将所有代码放在后台线程上,但这没有区别 - 事实上,当代码没有在后台线程上执行时 FileList 给我这个(null)而不是这个()

In the processFiles method, I've tried placing all of the code on the background thread, but this makes no difference - as a matter of fact, when the code is not executed on the background thread FileList gives me this (null) instead of this ( ).

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

Could my problem have to do with thread management or memory allocation? Please note that I am using ARC.

编辑#2: FileList 变量是我的 @interface 中定义的NSMutableArray,并在 - (id)init 方法,然后调用 processFiles 方法。

Edit #2: The FileList variable is an NSMutableArray defined in my @interface and initialized in the -(id)init method before calling the processFiles method.

编辑#3:使用断点测试我的代码时,我发现以下 for-loop 永远不会被运行 - 甚至一次都没有。这让我相信:

A.正确的目录没有连接到
B. iCloud无法看到目录中的文件

C.我的NSMetadataQuery没有正确设置
D.完全不同的东西

Edit #3: When testing my code with breakpoints I found that the following for-loop never gets run through - not even once. This leads me to believe that:
A. The proper directory isn't being connected with
B. iCloud can't see the files in the directory
C. My NSMetadataQuery isn't being setup properly D. Something completely different

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

Here's the code that starts the for-loop which never gets run:

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


推荐答案

<我解决了我的问题。获取iCloud中的文件列表只需要正确定义,分配和初始化属性。 SAE 的回答和这个 SO帖子帮我解决了我的问题并创建了这个名为 iCloud Document Sync 的GitHub Repo。 iCloud Document Sync类将整个iCloud Document存储过程简化为几行代码。与此处相关联的提交修复了我的问题。

I've solved my problem. Getting the list of files in iCloud was just a matter of correctly defining, allocating, and initializing properties. SAE's answer and this SO posting helped me solve my problem and create this GitHub Repo called iCloud Document Sync. The iCloud Document Sync class simplifies the whole iCloud Document storage process down to a few lines of code. The commit linked here fixes the issues from my question.

这篇关于iCloud NSMetadata查询结果为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆