0

如果您创建一个 vanilla 项目并将其作为您的应用程序委托的实现:

@interface TESTAppDelegate ()
@property (nonatomic, strong) NSMetadataQuery *query;
@end

@implementation TESTAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchProgressed:) name:NSMetadataQueryGatheringProgressNotification object:nil];

    NSMutableArray *predicates = [@[] mutableCopy];
    #define add(format, ...) { \
        [predicates addObject:[NSPredicate predicateWithFormat:format, ##__VA_ARGS__]]; \
    }

    //Toggle which of these lines are commented to experiment with breaking the query
    //add(@"kMDItemKind like[c] %@", @"*"); //Works
    //add(@"(kMDItemContentType != 'com.apple.mail.emlx.part')"); //Works
    //add(@"(kMDItemContentType == 'public.data')"); //Works
    //add(@"kMDItemFSName like[c] %@", @"*"); //DOES NOT WORK
    add(@"kMDItemFSName like[c] %@", @"*Nashville*"); //works...

    self.query = [[NSMetadataQuery alloc] init];
    [_query setPredicate:predicates.count > 1? [NSCompoundPredicate andPredicateWithSubpredicates:predicates] : predicates.lastObject];
    [_query setSearchScopes:@[[@"~/Downloads" stringByExpandingTildeInPath]]];
    NSLog(@"Query %@", [_query startQuery]? @"started" : @"could NOT start!");
}

- (void)searchProgressed:(NSNotification *)note
{
    NSLog(@"searchProgressed: %li", _query.resultCount);
}

@end

您应该能够在 NSMetadataQuery 上“最近”引入(狮子后)以下高度异常的行为:它显然不再有效。

如果您按原样运行应用程序,它应该记录类似的内容"searchProgressed 1204",表示查询找到的结果。但是,如果您在注释掉另一个谓词之后运行它,它什么也找不到。

我已经尝试了该行的许多变体,包括通配符或%K占位符的各种表述,将占位符更改LIKE[c]为其他形式,当然,使用诸如NSMetadataItemFSNameKeyNSMetadataItemURLKeykMDItemContentType. 除了上面的单一、最简单的情况外,没有任何效果

我一定错过了关于 NSMetadataQuery 的一些重要内容,我之前广泛使用它并取得了巨大的成功,因为否则每个人都会评论它是多么无用。

4

1 回答 1

2

我只是在猜测,希望这不会违反 StackOverflow 的规则:

我想知道 Apple 是否不希望 Spotlight 用于完全列出目录的内容——这可能是一种非常低效的方式——所以他们过滤掉了“过于宽泛”的查询。如果您将“<em>”更改为其他内容,例如“F”,它会起作用吗?

'-startQuery' 返回一个 BOOL,你看到它返回什么了吗?

于 2014-01-10T23:38:30.543 回答