Graphics Programs Reference
In-Depth Information
URL:dbURL
options:nil
error:&error]) {
[NSException raise:@"Open failed"
format:@"Reason: %@", [error localizedDescription]];
}
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:psc];
[context setUndoManager:nil];
}
return self;
}
This code - which is nearly the same as the code in
Chapter 16
- sets up the context for
inserting and fetching objects. This context works with the persistent store coordinator
and its SQLite persistent store to save and load objects to the filesystem.
Now on to implementing the two new methods on the store. First, import
RSSItem.h
in-
to
BNRFeedStore.m
.
#import "RSSItem.h"
Then, in
BNRFeedStore.m
, implement
markItemAsRead:
to insert a new
Link
entity into Core Data with the URL of the
RSSItem
's
link
.
- (void)markItemAsRead:(RSSItem *)item
{
// If the item is already in Core Data, no need for duplicates
if ([self hasItemBeenRead:item])
return;
// Create a new Link object and insert it into the context
NSManagedObject *obj = [NSEntityDescription
insertNewObjectForEntityForName:@"Link"
inManagedObjectContext:context];
// Set the Link's urlString from the RSSItem
[obj setValue:[item link] forKey:@"urlString"];
// immediately save the changes
[context save:nil];
}
In
BNRFeedStore.m
, implement
hasItemBeenRead:
to return
YES
if the
RSSItem
passed as an argument has its
link
stored in Core Data.
- (BOOL)hasItemBeenRead:(RSSItem *)item
{
// Create a request to fetch all Link's with the same urlString as
// this items link
NSFetchRequest *req = [[NSFetchRequest alloc] initWithEntityName:@"Link"];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"urlString like %@",
[item link]];