Graphics Programs Reference
In-Depth Information
The format string for a predicate can be very long and complex. Apple's
Predicate Pro-
gramming Guide
is a complete discussion of what is possible.
Predicates can also be used to filter the contents of an array. So, even if you had already
fetched the
allItems
array, you could still use a predicate:
NSArray *expensiveStuff = [allItems filteredArrayUsingPredicate:p];
Adding and deleting items
This handles saving and loading, but what about adding and deleting? When the user
wants to create a new
BNRItem
, you will not allocate and initialize this new
BNRItem
.
Instead, you will ask the
NSManagedObjectContext
to insert a new object from the
BNRItem
entity. It will then return an instance of
BNRItem
. In
BNRItemStore.m
,
edit the
createItem
method.
- (BNRItem *)createItem
{
BNRItem *p = [[BNRItem alloc] init];
double order;
if ([allItems count] == 0) {
order = 1.0;
} else {
order = [[allItems lastObject] orderingValue] + 1.0;
}
NSLog(@"Adding after %d items, order = %.2f", [allItems count], order);
BNRItem *p = [NSEntityDescription insertNewObjectForEntityForName:@"BNRItem"
inManagedObjectContext:context];
[p setOrderingValue:order];
[allItems addObject:p];
return p;
}
When a user deletes a
BNRItem
, you must inform the context so that it is removed from
the database. In
BNRItemStore.m
, add the following code to
removeItem:
.
- (void)removeItem:(BNRItem *)p
{
NSString *key = [p imageKey];
[[BNRImageStore sharedStore] deleteImageForKey:key];
[context deleteObject:p];
[allItems removeObjectIdenticalTo:p];
}