【问题标题】:How to monitor local documents directory NSMetaDataQuery如何监控本地文档目录 NSMetaDataQuery
【发布时间】:2011-12-31 18:55:44
【问题描述】:

使用 iCloud,我像这样设置了 MetaDataQuery:

[query setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];

如何为我的普通本地文档目录设置相同的 MetaDataQuery?

以下代码将为我提供目录中文件的静态列表 - 但我正在寻找一种更动态的方式,以便可以使用 NSMetadataQueryDidUpdateNotification

这是我在文档目录中查找文件的静态代码:

NSArray* localDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:
                           [self documentsDirectory] error:nil];

NSArray *onlyPQs = [localDocuments filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.pq'"]];

NSLog(@"local file count: %i", [onlyPQs count]);

for (int i=0; i < [onlyPQs count]; i++) {
    NSLog(@"LOCAL:%@", [onlyPQs objectAtIndex:i]);
}

【问题讨论】:

    标签: iphone objective-c ios cocoa-touch


    【解决方案1】:

    我一直在寻找这个问题的答案,根据文档 (Searching iCloud and the Desktop),这是不可能的:

    与桌面不同,iOS 应用程序的沙箱不可搜索 使用元数据类。为了搜索您的应用程序 沙箱,您将需要遍历沙箱文件中的文件 系统递归使用 NSFileManager 类。

    在 Mac OS X 上,定义了三个额外的范围:NSMetadataQueryUserHomeScopeNSMetadataQueryLocalComputerScopeNSMetadataQueryNetworkScope,可用于实现此目的。

    【讨论】:

      【解决方案2】:

      使类符合NSFilePresenter 并使用带有[NSFileCoordinator initWithFilePresenter:] 的实例。

      使用NSFileCoordinator 实例对目录 URL 执行协调读取,您的子类应通过以下回调接收更新(不完整列表,还有更多可用):

      • presentedSubitemDidChangeAtURL:
      • presentedSubitemDidAppearAtURL:
      • presentedSubitemAtURL:didMoveToURL:
      • accommodatePresentedSubitemDeletionAtURL:completionHandler:

      子项是正在读取的目录的后代。如果显示的目录本身是一个文件包,则应用一些额外的规则。查看NSFilePresenter 文档。

      编辑:由于 Apple 方面的一些错误,一些 NSFilePresenter 方法根本没有被调用。请参阅https://stackoverflow.com/a/19343823/193011https://www.objc.io/issues/10-syncing-data/icloud-document-store/#defective-notifications

      此方法要求使用NSFilePresenter 进行文件系统更改。通过其他方式提交的更改不会被拾取。

      【讨论】:

        【解决方案3】:

        您可以使用kqueue 监控本地文档目录。 Apple 提供了sample code(在 Objective-C 中)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-07-30
          • 2011-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多