Database Reference
In-Depth Information
Because we are handling the loading of our data in our reload method we only need to check to see
if the user selected “Yes” to use iCloud. If they did, we call
setiCloudOn:
and pass
YES
. Then we call
our reload method again.
Now we need to start adding some iCloud specific methods. At the bottom of the file, right above
the
@end
line, let's add a pragma mark and start adding methods.
The first method we will add will create an
NSMetadataQuery
object. An
NSMetadataQuery
is a query
that is specific to iCloud. It allows us to write a query that will first give us results, but it will also
continue to check for changes without calling it again and again. We could write this creation of this
query inline, but abstracting it out to its own method will allow for better readability of our overall
code. Let's write this method now.
-(NSMetadataQuery *)documentQuery {
NSMetadataQuery *query = [[NSMetadataQuery alloc] init];
if(query){
[query setSearchScopes:@[NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K LIKE %@",NSMetadataItemFSName
Key,[NSString stringWithFormat:@"*.%@",CT_EXTENSION]];
[query setPredicate:predicate];
}
return query;
}
First alloc and init an
NSMetadataQuery
object. We then check to be sure our object was created
successfully. Next we call the
setSearchScopes:
method on our query object and pass it an array
that consists of one value,
nSMetadataQueryUbiquitiousDocumentsScope
. Here we are telling our
query that we only want to search for files in the
Documents
directory of the application iCloud
container directories. We then write a predicate to confine our search even more. Our predicate says
we want all File System file names that are LIKE
*.ict
. We apply this newly created predicate by
calling
setPredicate
on our
NSMetadataQuery
object. Finally we return our query object.
Now we need to write two more methods. One will start our query and another will stop it.
-(void)queryiCloud {
[self stopiCloudQuery];
_query = [self documentQuery];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processiCloudFiles:)
name:NSMetadataQueryDidFinishGatheringNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(processiCloudFiles:)
name:NSMetadataQueryDidUpdateNotification object:nil];
[_query startQuery];
}
-(void)stopiCloudQuery {
if(_query){
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSMetadataQueryDidFinishGatheringNotification object:nil];
Search WWH ::
Custom Search