NSMetadataQueryDidUpdateNotification无法使其正常工作。包含代码片段

时间:2015-11-04 04:43:10

标签: ios objective-c icloud nsmetadataquery ubiquitycontainer

我无法使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];


        });


        });

2 个答案:

答案 0 :(得分: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.
}

答案 1 :(得分:0)

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

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

                                [self liveUpdate:note];

                            }];