Database Reference
In-Depth Information
- (BOOL)application:(NSApplication*)theApplication
openFile:(NSString*)filename
{
NSDictionary *metadata = [NSDictionary dictionaryWithContentsOfFile:filename];
NSString *objectIDString = [metadata valueForKey:(id)kPPObjectID];
NSURL *objectURI = [NSURL URLWithString:objectIDString];
NSPersistentStoreCoordinator *coordinator;
coordinator = [[self managedObjectContext] persistentStoreCoordinator];
NSManagedObjectID *objectID;
objectID = [coordinator managedObjectIDForURIRepresentation:objectURI];
NSManagedObject *recipe = [[self managedObjectContext] objectWithID:objectID];
if
(!recipe)
return
NO;
dispatch_async(dispatch_get_main_queue(), ^{
NSArray *array = [NSArray arrayWithObject:recipe];
[[self recipeArrayController] setSelectedObjects:array];
});
return
YES;
}
In our application delegate, we need to add the method
-(BOOL)application:openFile:
that will be called when the operating system attempts to open one of our
metadata files. In that method, we load the metadata file into an
NSDictionary
and retrieve the
URIRepresentation
of the
NSManagedObjectID
. With the
NSManagedOb-
jectID
in hand, we can load the represented
Recipe
entity and display it to the
user. Since we want to return from this method as quickly as possible (the
operating system is waiting on an answer), we display the recipe
after
we
return from this method.
To do that, we wrap the call to display the recipe in a
dispatch_async
, which
updates the
recipeArrayController
with the selected recipe and allows the UI to
update. By doing a
dispatch_async
and putting the execution block onto the main
queue, we are effectively telling the OS to please run the block right after the
current runloop completes.
With that code in place, we can select a recipe from Spotlight, and our
application opens with the correct recipe selected. The first part of our OS
integration is now in place.
9.2
Integrating with Quick Look
There are two different ways to implement Quick Look. The application can
generate images as part of the data bundle, or a generator can be written that