Database Reference
In-Depth Information
CTMetadata *metadata = [document metadata];
NSURL *fileURL = [document fileURL];
UIDocumentState state = [document documentState];
NSFileVersion *version = [NSFileVersion currentVersionOfItemAtURL:fileURL];
NSLog(@"Loaded file %@",[document fileURL]);
[document closeWithCompletionHandler:^(BOOL success) {
if(!success){
NSLog(@"There was an error closing the document at %@",fileURL);
}
dispatch_async(dispatch_get_main_queue(), ^{
[self addOrUpdateEntryWithURL:fileURL metadata:metadata state:state version:version];
});
}];
}];
}
The first line allocates a CTDocument and calls the method initWithFileURL :. This is a method that is
provided by the UIDocument sub class. Now that we have the document object we need to open it.
This is done by calling the openWithCompletionHandler : method on our CTDocument . This method is
also provided by the UIDocument subclass.
We first check for success. If we are unsuccessful, then we return out of the method. If we are successful,
we create a CTMetadata object by assigning the metadata object from the document to our local
metadata object. We grab the NSURL of the document, the document state, and the file version of the
document as well. We then close the document because all we need is the metadata at this time.
It is important to remember that any document you open must be closed before it can be opened
again. Be very careful to make sure you always close your documents when you are done with them.
We close the document by calling closeWithCompletionHandler:. We also check for success in this
method although even if it fails we still want to carry on with the rest of the method. Next we call
addOrUpdateEntryWithURL:metadata:state:version : on the main thread by calling it inside a
dispatch_async black and passing it the dispatch_get_main_queue() parameter. Now let's write our
method addOrUpdateEntryWithURL:metadata:state:version: .
-(void)addOrUpdateEntryWithURL:(NSURL *)fileURL metadata:(CTMetadata *)metadata
state:(UIDocumentState)state version:(NSFileVersion *)version {
NSInteger index = [self indexOfEntryWithFileURL:fileURL];
if(index == NSNotFound){
CTEntry *entry = [[CTEntry alloc] initWithFileURL:fileURL metadata:metadata state:state
andVersion:version];
[_entries addObject:entry];
[_entries sortUsingComparator:^NSComparisonResult(CTEntry *entry1, CTEntry *entry2) {
NSComparisonResult result = [[[entry1 metadata] displayName]
compare:[[entry2 metadata] displayName]];
NSLog(@"results is %d",result);
return result;
}];
 
Search WWH ::




Custom Search