簡體   English   中英

NSMetadataQueryDidUpdateNotification,無法使其正常工作。 包含代碼段

[英]NSMetadataQueryDidUpdateNotification, can't get this to work. Code Snippet included

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


        });


        });

希望這對您有幫助

嘗試用有關通知的代碼替換,元數據查詢應在主隊列上啟動,您做對了:)

    [[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.
}

解決方案是像這樣強烈引用通知塊

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

                                [self liveUpdate:note];

                            }];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM