我使用NSMetadataQuery
在我的项目中实现了聚光灯功能。收到NSMetadataQueryDidFinishGatheringNotification
通知后,我在格式化NSMetadataItem
NSMetadataQuery
后调用tableView方法的reloadData方法。我面临两个问题。
[query startQuery]
花费了太多时间。NSMetadataQueryDidUpdateNotification
; 当我使用NSArrayController
代替NStableViewDataSource
并将其与query.results
绑定时,搜索速度很快。因为我需要格式化NSMetadataItem
所以我无法使用它。
任何人都可以回答如何接收批量通知以获得更好的NSMetaDataQuery
效果。或任何其他方式来改善[query startQuery]
以下是我实施的代码。
- (void)initiateSearch:(NSString *)seString
{
self.metadataSearch=[[[NSMetadataQuery alloc] init] autorelease];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(queryDidUpdatesfsdf:)
name:NSMetadataQueryDidUpdateNotification
object:metadataSearch];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(initalGatherComplete:)
name:NSMetadataQueryDidFinishGatheringNotification
object:metadataSearch];
NSArray *terms = [seString componentsSeparatedByString:@","];
NSMutableArray *pridicateArray = [NSMutableArray array];
NSPredicate *searchPredicate;
for(NSString *term in terms) {
if([term length] == 0)
{
continue;
}
NSString *trimmedString = [[NSString alloc] initWithFormat:@"*%@*",[term stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
NSPredicate *p = [NSPredicate predicateWithFormat:@"(kMDItemFSName like[cd] %@)",trimmedString];
[pridicateArray addObject:p];
[trimmedString release];
}
if ([pridicateArray count] == 1) {
searchPredicate= [pridicateArray objectAtIndex:0];
}
else
{
searchPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:pridicateArray];
}
[metadataSearch setPredicate:searchPredicate];
NSArray *searchScopes;
searchScopes=[NSArray arrayWithObjects:NSMetadataQueryUserHomeScope,nil];
[metadataSearch setSearchScopes:searchScopes];
NSSortDescriptor *sortKeys=[[[NSSortDescriptor alloc] initWithKey:(id)kMDItemDisplayName
ascending:YES] autorelease] ;
[metadataSearch setSortDescriptors:[NSArray arrayWithObject:sortKeys]];
[metadataSearch setNotificationBatchingInterval:5.0];
[metadataSearch startQuery];
}
- (void)queryDidUpdatesfsdf:sender;
{
NSLog(@"A data batch has been received");
}
- (void)initalGatherComplete:sender;
{
[metadataSearch stopQuery];
p_Thread = [[NSThread alloc] initWithTarget:self selector:@selector(dosome:) object:metadataSearch];
[p_Thread start];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMetadataQueryDidUpdateNotification
object:metadataSearch];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMetadataQueryDidFinishGatheringNotification
object:metadataSearch];
}
-(void)dosome: (NSMetadataQuery *)metadataresult
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
fileListArray = [[NSMutableArray alloc] init];
[fileListArray removeAllObjects];
NSUInteger i=0;
for (i=0; (i < [metadataresult resultCount] && ![p_Thread isCancelled]); i++) {
NSMetadataItem *theResult = [metadataresult resultAtIndex:i];
NSString *displayName = [theResult valueForAttribute:(NSString *)kMDItemFSName];
NSString *displayPath = [theResult valueForAttribute:(NSString *)kMDItemPath];
NSString *fileSize = [theResult valueForAttribute:(NSString *)kMDItemFSSize];
FileList *fileList = [[FileList alloc] init];
[fileList setName:displayName];
[fileList setIsChecked:NO];
[fileList setPath:displayPath];
[fileList setLastModified:[dateFormatter stringFromDate:[theResult valueForAttribute:(NSString *)kMDItemContentModificationDate]]];
[fileList setFileSize:[IBUtility formatedFileSizeForOutlineView:fileSize]];
[fileList setMSize:[fileSize longLongValue]];
[[self fileListArray] addObject:fileList];
if (i%50 == 0 && ![p_Thread isCancelled]) {
[searhFinderTableView performSelectorOnMainThread:@selector(reloadData) withObject:Nil waitUntilDone:YES];
}
}
self.metadataSearch=nil;
if ([[self fileListArray] count] == 0) {
[mSearchingLabel setStringValue:@"Items not found"];
NSRect textFieldFrame = [mSearchingLabel frame];
[mSearchingLabel setFrame:NSMakeRect(mContinuousProgress.frame.origin.x,textFieldFrame.origin.y, textFieldFrame.size.width,textFieldFrame.size.height)];
}
else if(![p_Thread isCancelled])
{
[searhFinderTableView performSelectorOnMainThread:@selector(reloadData) withObject:Nil waitUntilDone:YES];
[mSearchingLabel setHidden:YES];
}
[mContinuousProgress stopAnimation:self];
[dateFormatter release];
[pool release];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [fileListArray count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
FileList *fileList = [fileListArray objectAtIndex:rowIndex];
if ([[aTableColumn identifier] isEqualToString:@"filePath"])
{
return [fileList valueForKey:@"path"];
}
else if([[aTableColumn identifier] isEqualToString:@"isSelected"])
{
return [NSNumber numberWithBool:fileList.isChecked];
}
else if([[aTableColumn identifier] isEqualToString:@"fileSize"])
{
return [fileList valueForKey:@"fileSize"];
}
else if([[aTableColumn identifier] isEqualToString:@"lastModified"])
{
return [fileList valueForKey:@"lastModified"];
}
return nil;
}