Graphics Programs Reference
In-Depth Information
NSArray *documentDirectories =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
// Get one and only document directory from that list
NSString *documentDirectory = [documentDirectories objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"items.archive"];
return [documentDirectory stringByAppendingPathComponent:@"store.data"];
}
When the BNRItemStore is initialized, it needs to set up the NSManagedOb-
jectContext and a NSPersistentStoreCoordinator . The persistent store co-
ordinator needs to know two things: “What are all of my entities and their attribute and re-
lationships?” and “Where am I saving and loading data from?” To answer these questions,
we need to create an instance of NSManagedObjectModel to hold the entity informa-
tion of Homepwner.xcdatamodeld and initialize the persistent store coordinator with
this object. Then, we will create the instance of NSManagedObjectContext and spe-
cify that it use this persistent store coordinator to save and load objects.
In BNRItemStore.m , update init .
- (id)init
{
self = [super init];
if (self) {
NSString *path = [self itemArchivePath];
allItems = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
if (!allItems)
allItems = [[NSMutableArray alloc] init];
// Read in Homepwner.xcdatamodeld
model = [NSManagedObjectModel mergedModelFromBundles:nil];
NSPersistentStoreCoordinator *psc =
[[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
// Where does the SQLite file go?
NSString *path = [self itemArchivePath];
NSURL *storeURL = [NSURL fileURLWithPath:path];
NSError *error = nil;
if (![psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil
error:&error]) {
[NSException raise:@"Open failed"
format:@"Reason: %@", [error localizedDescription]];
}
// Create the managed object context
context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:psc];
 
Search WWH ::




Custom Search