【发布时间】:2014-01-30 02:27:51
【问题描述】:
如果您创建一个普通项目并将其作为您的应用委托的实现:
@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] 占位符更改为其他形式,当然还有使用NSMetadataItemFSNameKey、@987654326 之类的东西@ 和 kMDItemContentType。 除了上述单一、最简单的情况外,没有其他方法可以工作。
我一定错过了 NSMetadataQuery 的一些重要内容,我之前曾广泛使用它并取得了巨大的成功,否则每个人都会评论它是多么无用。
【问题讨论】:
-
您的应用是否在沙盒中运行?
标签: cocoa nsmetadataquery