Graphics Programs Reference
In-Depth Information
// The managed object context can manage undo, but we don't need it
[context setUndoManager:nil];
}
return self;
}
Before,
BNRItemStore
would write out the entire
NSMutableArray
of
BNRItem
s
when you asked it to save using keyed archiving. Now, you will have it send the message
save:
to the
NSManagedObjectContext
. The context will update all of the records
in
store.data
with any changes since the last time it was saved. In
BNRItemStore.m
, change
saveChanges
.
- (BOOL)saveChanges
{
NSString *path = [self itemArchivePath];
return [NSKeyedArchiver archiveRootObject:allItems
toFile:[self itemArchivePath]];
NSError *err = nil;
BOOL successful = [context save:&err];
if (!successful) {
NSLog(@"Error saving: %@", [err localizedDescription]);
}
return successful;
}
Note that this method is already called when the application is moved to the background.
NSFetchRequest and NSPredicate
In this application, we will fetch all of the
BNRItem
s in
store.data
the first time we
need them. To get objects back from the
NSManagedObjectContext
, you must pre-
pare and execute an
NSFetchRequest
. After a fetch request is executed, you will get
an array of all the objects that match the parameters of that request.
A fetch request needs an entity description that defines which entity you want to get ob-
jects from. To fetch
BNRItem
instances, you specify the
BNRItem
entity. You can also
set the request's
sort descriptors
to specify the order of the objects in the array. A sort
descriptor has a key that maps to an attribute of the entity and a
BOOL
that indicates if the
order should be ascending or descending. We want to sort the returned
BNRItem
s by
orderingValue
in ascending order. In
BNRItemStore.h
, declare a new method.
- (void)loadAllItems;