ios - 处理基于 NSMetadataQueryDidUpdateNotification 非 UIDocument 的 iCloud 文件时出现问题

标签 ios ios6 icloud nsmetadataquery icloud-api

在尝试解决这个问题时,我已经很头疼了:在我当前的应用程序中,我使用 iCloud 来存储我的文档 - 但我无法使用基于 UIDocument 的数据存储。因此,我的方法是遵循 WWDC 2012 第 237 条(高级 iCloud 文档存储)并将我的文档视为鞋盒应用程序。

当我构建 NSMetadataQuery 来监视 iCloud 文档文件夹中的更改以监视文件更改时,真正的问题发生了。我已经设置了查询,使用 NSMetadataQueryDidFinishGatheringNotification 的观察者启动它,当它触发时,我启用查询更新并为 NSMetadataQueryDidUpdateNotification 设置观察者。后者仅对一个文件更改有效一次,并且我不知道如何让它在相同或其他文件的后续更改中重复触发。以下是一些代码部分:

- (void)startMonitoringDocumentsFolder: (id)sender
{
    if (self.queryDocs != nil)
        return;

    NSLog(@"startMonitoringDocumentsFolder:");

    self.queryDocs = [[NSMetadataQuery alloc] init];
    [queryDocs setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];

    NSPredicate*    pred = [NSPredicate predicateWithFormat:@"%K like '*.nwa' or %K like '*.NWA'", NSMetadataItemFSNameKey, NSMetadataItemFSNameKey];

    [self.queryDocs setPredicate:pred];

    NSNotificationCenter*   center    = [NSNotificationCenter defaultCenter];
    NSOperationQueue*       mainQueue = [NSOperationQueue mainQueue];

    // I'm using the blocks version because I'm planning to setup observers for the same tracking name...
    self.observeGatherDocs = [center addObserverForName: NSMetadataQueryDidFinishGatheringNotification
                                                 object: self.queryDocs
                                                  queue: mainQueue
                                             usingBlock: ^(NSNotification* note)
    {
        [self queryDidFinishGathering:note];
    }];
    [self.queryDocs startQuery];
}

- (void)queryDidFinishGathering:(NSNotification *)notification
{
    NSLog(@"queryDidFinishGathering:");
    NSMetadataQuery*    query = [notification object];

    if (query == queryDocs)
    {
        NSLog(@"--> for queryDocs");
        NSNotificationCenter*   center    = [NSNotificationCenter defaultCenter];
        NSOperationQueue*       mainQueue = [NSOperationQueue mainQueue];

        [center removeObserver: observeGatherDocs];
        self.observeGatherDocs = nil;
        self.observeUpdateDocs = [center addObserverForName: NSMetadataQueryDidUpdateNotification
                                                     object: query
                                                      queue: mainQueue
                                                 usingBlock: ^(NSNotification* note)
        {
            [self queryDidUpdateDocumentsFolder:note];
        }];

        [query enableUpdates];
    }
}

- (void)queryDidUpdateDocumentsFolder:(NSNotification *)notification
{
    NSLog(@"queryDidUpdateDocumentsFolder:");
    NSMetadataQuery*    query = [notification object];

    [query disableUpdates];

    for (NSMetadataItem* item in [query results])
    {
        // find and handle file changes here w/out any command calls to the query itself
        ...
    }

    if (self.needDownload)
        [self triggerDownloadThread];

    // ---- BEGIN code part added to solve the problem ---- //
    // re-create the observer
    NSNotificationCenter*   center    = [NSNotificationCenter defaultCenter];
    NSOperationQueue*       mainQueue = [NSOperationQueue mainQueue];

    [center removeObserver: observeUpdateDocs];
    self.observeUpdateDocs = [center addObserverForName: NSMetadataQueryDidUpdateNotification
                                                 object: query
                                                  queue: mainQueue
                                             usingBlock: ^(NSNotification* note)
    {
        [self queryDidUpdateDocumentsFolder:note];
    }];
    // ---- END code part added to solve the problem ---- //

    [query enableUpdates];
    // that's all with the query, am I missing something?
}

非常感谢这里的任何建议。

它的真正作用:

  1. 当我直接(通过覆盖)修改 iCloud 文档容器中的文件时 - 没有任何操作,查询不会触发。

  2. 当我通过序列 setUbiquitous:NO 修改文件,写入本地文件,setUbiquitous:YES 查询触发一次,我检测到更改,当我再次更改同一文件时,不会再调用查询处理程序。

因此,尽管我在 Apple 文档中发现像上述 1. 这样写入 iCloud 文件的方式适用于新文件,但似乎还需要修改现有文件。

但是,无论如何,我怎样才能让查询机制重复运行呢?

干杯...

最佳答案

经过更多的努力调查后,我找到了一个非凡的解决方案。因此,如果其他人绊倒,我只是分享我的经验:

  1. 观察者实例变量应保留为保留引用(或使用 ARC 时为强引用)
  2. 在启用更新之前,必须重新创建观察者。

这似乎就是全部了。目前看来它有效。

关于ios - 处理基于 NSMetadataQueryDidUpdateNotification 非 UIDocument 的 iCloud 文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14045904/

相关文章:

ios - 辛奇 iOS 应用程序 : Identifying if the app receives video or voice call

objective-c - UIButton 在 iOS 5.x 中不起作用,在 iOS 6.x 中一切正常

ios - 如何在 Xcode 中添加此约束?看来我的 relatedBy : argument is wrong

ios - 替代 iOS 10.0 核心数据中的 NSPersistentStoreUbiquitousContentNameKey 键

ios - 如何使用文件管理器将 iCloud 中的文件复制到应用程序沙箱

ios - 从嵌套完成 block 返回值

ios - Swift:异步回调

ios - Core Data 没有从 iCloud 下载所有数据

ios - GPPSignIn 共享实例 -> EXC_BAD_ACCESS(代码 = EXC_I386_GPFLT)

ios - CATextLayer 上来自 iOS 6 的不需要的垂直填充