Database Reference
In-Depth Information
In addition to this list, we need to add one more item that is not user-facing.
We want a way to link back to the recipe record in our Core Data repository
so if the user tries to open the metadata file, instead our application will open
and select the correct record. To do this, we use the
NSManagedObjectID
of the
recipe and store its
URIRepresentation
(which is actually an
NSURL
) as a string in
the metadata.
- (NSDictionary*)metadata;
{
NSMutableDictionary *metadataDict = [NSMutableDictionary dictionary];
[metadataDict setValue:[self name]
forKey:(id)kMDItemTitle];
[metadataDict setValue:[self desc]
forKey:(id)kMDItemTextContent];
[metadataDict setValue:[[self author] valueForKey:@
"name"
]
forKey:(id)kMDItemAuthors];
[metadataDict setValue:[self valueForKey:@
"imagePath"
]
forKey:kPPImagePath];
[metadataDict setValue:[self lastUsed] forKey:(id)kMDItemLastUsedDate];
[metadataDict setValue:[self valueForKey:@
"serves"
] forKey:kPPServes];
[metadataDict setValue:[NSString stringWithFormat:@
"Recipe: %@"
, [self name]]
forKey:(id)kMDItemDisplayName];
[metadataDict setValue:[[[self objectID] URIRepresentation] absoluteString]
forKey:kPPObjectID];
return
metadataDict;
}
Implementing the metadataName Method
Because we want users to be able to view the actual metadata files in the
Finder, the filenames should represent the recipe rather than an abstract
name. We use the
name
attribute of the recipe itself as the filename.
- (NSString*)metadataFilename;
{
return
[[self name] stringByAppendingPathExtension:@
"grokkingrecipe"
];
}
Generating and Updating the Metadata Files
Now that we have an implementation for generating the metadata per recipe,
we need to add the ability to populate these files and keep them up-to-date.
Ideally, we want to refresh the metadata files every time that the
NSManagedOb-
jectContext
is saved. To do this, we add a new
-save:
method to our
AppDelegate
and route all of our saves through it.