搜索iCloud文件Searching for iCloud Documents

文件有一个设备上的一个应用创建,通过相同的应用运行在用户的其他设备上。一个应用发现文档在一个iCloud容器目录,是通过使用一个元数据查询对象,这是一个NSMetadatQuery对象的实例。一个元数据查询对象为文件查找可用的iCloud容器目录。Documents created by an app on one device must be discoverable by the same app running on the user’s other devices. An app discovers documents in an iCloud container directory is by using a metadata query object, which is an instance of the NSMetadataQuery class. A metadata query object searches the available iCloud container directories for files matching the criteria you specify.

因为文件会在任意时间出现在一个容器目录中(或者从目录中消失),它经常会使场景开启一个队列,并让它运行。在这种情况下,你应该在你应用的一些主要部分创建查询对象,并在你应用的声明周期与该对象保持一个关联。Because documents can appear in a container directory (or disappear from the directory) at any time, it often makes sense to start a query and leave it running. In such a case, you should create the query object in some central part of your app and keep a reference to that object for the lifetime of your app.

开始你的文档的寻找Starting Your Search for Documents

STEMasterViewController类在你应用声明的开始创建一个元数据查询对象,并打开它。这个查询会立刻返回一个文档的初始设置,当发生改变时发送修改。The STEMasterViewController class creates a metadata query object early in the life of the app and starts it. This query returns an initial set of documents right away and sends later updates when changes occur.

因为查询在应用运行过程中不断运行,STEMasterViewController类持续地使用一个实例变量来存储一个查询对象。Because the query runs constantly while the app is running, the STEMasterViewController class uses an instance variable to store a query object persistently.

为元数据查询对象声明存储To declare the storage for the metadata query object
  1. 在项目导航中,选择STEMasterViewController.mIn the project navigator, select STEMasterViewController.m.

  2. 添加一个称为_query的私有实例变量来存储元数据查询对象。Add a private instance variable called _query to store the metadata query object.

    变量的类型是一个指向NSMetadataQuery的指针。你的类的顶部实现,应该如下:The type of the variable is a pointer to NSMetadataQuery. The top of your class implementation should now look like the following:

    @implementation STEMasterViewController {
        NSMutableArray *documents;
        NSMetadataQuery *_query;
    }
    @synthesize addButton;

你在应用启动循环期间初始化_query实例变量。You initialize the _query instance variable during the app launch cycle.

在初始化时配置并启动搜索查询To configure and start the search query at initialization time
  1. 在项目导航中,选择STEMasterViewController.mIn the project navigator, select STEMasterViewController.m.

  2. 定义一个新的被称为textDocumentQuery的方法来创建一个查询对象Define a new method called textDocumentQuery to create the query object.

    这个方法的实现创建一个新的NSMetadataQuery对象,并配置它,看着像只是为在Document目录中的文档。这个方法的实现如下:The implementation of this method creates a new NSMetadataQuery object and configures it to look only for text documents in the Documents directory of the app’s iCloud containers. The implementation of this method is as follows:

    - (NSMetadataQuery*)textDocumentQuery {
        NSMetadataQuery* aQuery = [[NSMetadataQuery alloc] init];
        if (aQuery) {
            // Search the Documents subdirectory only.
            [aQuery setSearchScopes:[NSArray
                        arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
     
            // Add a predicate for finding the documents.
            NSString* filePattern = [NSString stringWithFormat:@"*.%@",
                        STEDocFilenameExtension];
            [aQuery setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE %@",
                        NSMetadataItemFSNameKey, filePattern]];
        }
     
        return aQuery;
    }

  3. 定义一个新的setupAndStartQuery方法来打开查询。Define a new method called setupAndStartQuery to start the query.

    这个方法检索元数据查询对象,配置通知处理程序,打开查询。这个方法的实现如下:The setupAndStartQuery method retrieves the metadata query object, configures the notification handlers, and starts the query. The implementation of this method is as follows:

    - (void)setupAndStartQuery {
       // Create the query object if it does not exist.
       if (!_query)
          _query = [self textDocumentQuery];
     
       // Register for the metadata query notifications.
       [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(processFiles:)
            name:NSMetadataQueryDidFinishGatheringNotification
            object:nil];
       [[NSNotificationCenter defaultCenter] addObserver:self
            selector:@selector(processFiles:)
            name:NSMetadataQueryDidUpdateNotification
            object:nil];
     
       // Start the query and let it run.
       [_query startQuery];
    }

  4. 在文件的顶部添加一个setupAndStartQuery方法向前的声明Add a forward declaration of the setupAndStartQuery method at the top of the file.

  5. 修改awakeFromNib方法来调用setupAndStartQuery方法Update the awakeFromNib method to call the setupAndStartQuery method.

    你的awakeFromNib方法应该开起来类似如下:Your awakeFromNib method should now look similar to the following:

    - (void)awakeFromNib {
       [super awakeFromNib];
     
       if (!documents)
          documents = [[NSMutableArray alloc] init];
     
       self.navigationItem.leftBarButtonItem = self.editButtonItem;
       [self setupAndStartQuery];
    }

查询结果的过程Processing the Search Results

搜索结果在两个阶段传递:初始的收集阶段和修改阶段。初始阶段在你打开一个元数据查询就会立刻发生并且是第一组结果返回。之后,查询对象发送修改通知只在目录中的文件改变时。对于两个阶段,元数据查询结果包括所有在特定位置找到的文件。Search results are delivered in two phases: the initial gathering phase and the update phase. The initial gathering phase occurs immediately after you start a metadata query and is the first set of results to be returned. Thereafter, the query object sends update notifications only when the files in the directory change. For both the initial gathering phase and for updates, the metadata query results include all of the files currently found in the specified location.

简单文件编辑应用那个中,使用元数据查询通知来修改它的文档列表,刷新它的用户接口。The Simple Text Editor app uses the metadata query notifications to update its list of documents and refresh its user interface accordingly.

处理文档列表To process the list of documents
  1. 在项目导航中,选择STEMasterViewController.mIn the project navigator, select STEMasterViewController.m.

  2. 添加如下客户processFiles:方法的实现Add the following implementation of the custom processFiles: method.

    这个方法创建一个发现的文件的列表,过滤应该被隐藏的文件。当处理一个搜索查询的结果,当你处理文件列表时,暂时禁用更新。这可以防止查询结果在你遍历它时发生改变。This method builds a list of discovered files, filtering out files that should be hidden. When processing the results of a search query, disable updates temporarily while you process the list of files. This prevents the query results from changing while you are iterating through them.

    - (void)processFiles:(NSNotification*)aNotification {
        NSMutableArray *discoveredFiles = [NSMutableArray array];
     
        // Always disable updates while processing results.
        [_query disableUpdates];
     
        // The query reports all files found, every time.
        NSArray *queryResults = [_query results];
        for (NSMetadataItem *result in queryResults) {
            NSURL *fileURL = [result valueForAttribute:NSMetadataItemURLKey];
            NSNumber *aBool = nil;
     
            // Don't include hidden files.
            [fileURL getResourceValue:&aBool forKey:NSURLIsHiddenKey error:nil];
            if (aBool && ![aBool boolValue])
                [discoveredFiles addObject:fileURL];
        }
     
        // Update the list of documents.
        [documents removeAllObjects];
        [documents addObjectsFromArray:discoveredFiles];
        [self.tableView reloadData];
     
        // Reenable query updates.
        [_query enableUpdates];
    }

创建并运行你的应用Building and Running Your App

在这一点上,你应该有所有你需要构建并运行完整的应用程序。你应该能够在iCloud创建新文档,打开现有文档,编辑这些文档的内容。如果您有多个相关设备配置概要文件,你应该也可以插入每个设备,看到文件上创建一个出现在另一个。At this point, you should have everything you need to build and run the complete app. You should be able to create new documents in iCloud, open existing documents, and edit the contents of those documents. If you have multiple devices associated with the provisioning profile, you should also be able to plug in each device and see the files created on one show up on the other.

如果应用没有如期运行,你可以使用“Troubleshooting” 进行故障排除。If the app does not behave as you expect, you can troubleshoot the problem using the information in “Troubleshooting.”

扼要重述Recap

当你完成搜寻的实现,你已经完成了你的第三个iOS应用(一直在说的iCloud应用),祝贺!Now that you have completed the implementation of searching, you have completed your third iOS app. Congratulations!

花一会来考虑iCloud将如何影响你的应用框架。大部分需要支持iCloud的工作是在你的应用程序的数据部分。当你用一个UIDocument对象来管理你的文件,你所要做的工作量其实很小。Take a moment to think about how iCloud impacted your overall app architecture. Most of the work needed to support iCloud is in the data portion of your app. And when you use a UIDocument object to manage your files, the amount of work you have to do is actually small.

转载于:https://www.cnblogs.com/zyingn/articles/iOS_translation19.html

iCloud5_Building and Running Your App相关推荐

  1. app engine_App Engine中的Google Services身份验证,第2部分

    app engine 在本教程的第一部分中, 我介绍了如何使用OAuth进行Google API服务的访问/身份验证. 不幸的是,正如我稍后发现的那样,我使用的方法是OAuth 1.0,显然现在Goo ...

  2. App Engine中的Google Services身份验证,第2部分

    在本教程的第一部分中, 我描述了如何使用OAuth进行Google API服务的访问/身份验证. 不幸的是,正如我稍后发现的那样,我使用的方法是OAuth 1.0,显然现在Google正式弃用了OAu ...

  3. 使用Redux在React Navigation App中管理状态

    by Andrea Bizzotto 通过安德里亚·比佐托(Andrea Bizzotto) 使用Redux在React Navigation App中管理状态 (Managing State in ...

  4. 浅谈APP的回收和重启机制

    /   今日科技快讯   / 近日不少用户发现微信开始内测在企业号入口增加微信公众号内容的推荐.具体情况是,在微信的服务通知的企业微信企业状态提醒中,会推送同事阅读较多的文章内容,而且微信还会选择性推 ...

  5. Android App启动流程详解

    前言:在之前的文章中已经写了apk的打包流程.安装流程,今天就是梳理一下apk系列的最后的流程--app启动流程.经过今天的梳理以后咱们就可以对apk包是怎么编译生成的.apk是怎么被安装到安卓手机的 ...

  6. iOS“伪后台“机制下如何保持APP一直运行在后台

    最近在做番茄钟的功能.首先简单介绍一下番茄钟吧,就是25分钟工作番茄工作法.先说一下** 番茄工作法 **: 番茄工作法是简单易行的时间管理方法,是由弗朗西斯科·西里洛于1992年创立的一种相对于GT ...

  7. iOS App由生到死的过程

    万物都会经历由生到死的过程,人不例外,当然App也是如此,此谓App的声明周期.具体来说App的声明周期就是App从启动到关闭这一过程中发生的一系列事件.由于iOS4中引入了多任务功能,App能够在后 ...

  8. The App Launch Cycle

    When your app is launched, it moves from the not running state to the active or background state, tr ...

  9. iOS 通过定位启动App

    本文介绍iOS定位的三种情况:本文介绍iOS定位的三种情况: iOS前台定位以及不同版本(iOS 7.8.9)之间定位的基本使用和异同点 iOS后台(Background)的持续定位 APP未运行(N ...

最新文章

  1. 关于.NET玩爬虫这些事 【初码干货】
  2. python列表的排序方法_pythonlist排序的两种方法及实例
  3. SAP QM初阶之明明存在检验计划但是生成的检验批取不到?
  4. 【Flask】在g对象中存放List,实现在捕获异常后继续执行for循环
  5. EnjoyingSoft之Mule ESB开发教程第六篇:Data Transform - 数据转换
  6. MongoDB 空指针引用拒绝服务漏洞
  7. inputstream 初始化_如何完美回答面试官问的Mybatis初始化原理!
  8. 逻辑斯蒂回归_逻辑回归 - 3 逻辑回归模型
  9. mysql begin rollback_事务控制语句,begin,rollback,savepoint,隐式提交的SQL语句
  10. flink入门_Flink从入门到放弃-入门篇
  11. putty 使用perm密钥文件登陆堡垒机
  12. 【历史上的今天】5 月 31 日:Amiga 之父诞生;BASIC 语言的共同开发者出生;黑莓 BBM 停运
  13. AndroidVideoCache-视频边播放边缓存的代理策略
  14. PS 基本了解和图层
  15. Mysql 生成随机数字
  16. [NeRF]学习笔记(持续更新中)
  17. 【Google Chrome】reCAPTCHA did not get loaded.
  18. mmap内存映射原理
  19. 硕盟Type-C智能10合一多功能拓展坞|硕盟Type-C转换器
  20. C. Wilbur and Points(简单贪心加合法性判断)

热门文章

  1. 【配置】Spring Struts配置信息
  2. 记忆碎片---搭建php+apache+eclipse中的问题
  3. 2010年的最后一天,我又辞工(日记)
  4. jupyter notebook + 服务器中docker 使用配置
  5. PyTorch入坑(一)~(三): Tensor的概念,基本操作和线性回归
  6. 反向传播(Back Propagation)与神经网络(Neural Network)
  7. Linux文件的搜寻
  8. 必背单词_考研英语语法如何高效自学? 真题必背单词Day12
  9. OSG仿真案例(4)——创建烟雾粒子效果
  10. java显示字母数字组合_Java字母加数字组合比较大小