Database Reference
In-Depth Information
-(void)setPhoto:(UIImage *)photo {
if([[self.data photo] isEqual:photo])
return;
UIImage *oldPhoto = [self.data photo];
[self.data setPhoto:photo];
[self.metadata setThumbnail:[photo resizedImage:CGSizeMake(280, 280)
interpolationQuality:kCGInterpolationHigh]];
[self.undoManager setActionName:@"Photo Change"];
[self.undoManager registerUndoWithTarget:self selector:@selector(setPhoto:) object:oldPhoto];
}
-(NSString *)description {
return [[self.fileURL lastPathComponent] stringByDeletingPathExtension];
}
As you can see in each of our getter methods we access the property by calling self.data . This
is another mechanism of the lazy loading. It ensures that we have a data object and if we haven't
accessed the data object before it will cause it to be either decoded or instantiated as a blank
object.
The setter methods are a little different so we should go into more detail with these. We start off by
checking to see if the value is equal to the value we are passing in. If it is then we return out. This
keeps us from writing the same values over and over thus keeping our app performance efficient
and not inadvertently signaling system writes when no data has actually changed. So, if the value is
different, then we copy the old value into a variable that we will use with the undo manager (more on
that in a second). Then we set the property to the new value.
The next two methods work with the NSUndoManager . We start by calling the method setActionName:
and pass it a readable string for the action we are currently performing. We then call a second
method on NSUndoManager called registerUndoWithTarget:selector:object: . Our target is naturally
our self. For selector we pass in the selector that is currently performing the action. And for the
object we pass in the copy of the object, or the old value. What this does is tell the undo manager
that in order to undo what we just did we would want to call the setSomething method on our self
and pass it the old value.
Now, we won't actually use this functionality in our app, but I wanted to show you how easy it is to
implement. You can undo the last action by calling [self.undoManager undo] . You can do something
similar for redo.
The final thing in these accessor methods to notice is both in our setDisplayName and setPhoto:
methods. Because our metadata is derived from our data, we also set our metadata in these two
places. In setDisplayName we just apply the value to both the data and metadata. However, in our
setPhoto method we resize our image to create a thumbnail and set that image as our thumbnail
property in metadata.
Now that we have created our document model and our document, we have one last class to create
to make display our document previews a little easier.
 
Search WWH ::




Custom Search