0

我无法让 NSMetadataQueryDidUpdateNotification 工作。坚持了好几天。下面的代码是否有异常。

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0ul), ^{


    NSString* filePattern = [NSString stringWithFormat:@"*.%@", @"*"];

        NSMetadataQuery *aQuery = [[NSMetadataQuery alloc] init];
        aQuery.predicate = [NSPredicate predicateWithFormat: @"%K LIKE %@", NSMetadataItemFSNameKey, filePattern];

        [aQuery setSearchScopes:@[NSMetadataQueryUbiquitousDataScope]];
        [aQuery setValueListAttributes:@[NSMetadataUbiquitousItemPercentDownloadedKey, NSURLUbiquitousItemDownloadingStatusKey,NSURLUbiquitousItemIsDownloadingKey,NSURLUbiquitousItemDownloadRequestedKey]];

        _query = aQuery;

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                                 selector:@selector(liveUpdate:)
                                                                     name:NSMetadataQueryDidUpdateNotification
                                                                   object:aQuery];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(initalGatherComplete:)                                                                                          name:NSMetadataQueryDidFinishGatheringNotification object:aQuery];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gatherProgress:)                                                                                          name:NSMetadataQueryGatheringProgressNotification object:aQuery];

        [aQuery enableUpdates];

         dispatch_async(dispatch_get_main_queue(), ^{

        [aQuery startQuery];


        });


        });
4

2 回答 2

0

解决方案是像这样强烈引用通知块

    _notifqueryDidUpdate = [[NSNotificationCenter defaultCenter]addObserverForName:NSMetadataQueryDidUpdateNotification object:aQuery queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {

                                [self liveUpdate:note];

                            }];
于 2015-11-05T02:37:20.097 回答
0

希望这可以帮助你

尝试用这个关于通知的代码替换元数据查询应该在主队列上开始,你做对了:)

    [[NSNotificationCenter defaultCenter] addObserver:self  
selector:@selector(liveUpdate:) 
name:NSMetadataQueryDidUpdateNotification 
object:aQuery];

    [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(initalGatherComplete:)
name:NSMetadataQueryDidFinishGatheringNotification 
object:aQuery];

这是处理收集通知的示例

- (void)initialGatherComplete:(NSNotification*)notification
{
      //process here.
}
于 2015-11-04T05:12:49.187 回答