Added new initializer for starting from a seed database. Need to finish cleaning up API and add example app.

This commit is contained in:
Blake Watters
2011-03-04 07:16:52 -05:00
parent 6ad118b822
commit c6546feb70
4 changed files with 52 additions and 5 deletions

View File

@@ -24,14 +24,25 @@ extern NSString* const RKManagedObjectStoreDidFailSaveNotification;
NSObject<RKManagedObjectCache>* _managedObjectCache;
}
// The filename of the database backing this object store
@property (nonatomic, readonly) NSString* storeFilename;
// The full path to the database backing this object store
@property (nonatomic, readonly) NSString* pathToStoreFile;
// Core Data
@property (nonatomic, readonly) NSManagedObjectModel* managedObjectModel;
@property (nonatomic, readonly) NSPersistentStoreCoordinator* persistentStoreCoordinator;
/**
* Managed object cache provides support for automatic removal of objects pruned
* from a server side load. Also used to provide offline object loading
*/
@property (nonatomic, retain) NSObject<RKManagedObjectCache>* managedObjectCache;
/*
* This returns an appropriate managed object context for this object store.
* Because of the intrecacies of how CoreData works across threads it returns
* Because of the intrecacies of how Core Data works across threads it returns
* a different NSManagedObjectContext for each thread.
*/
@property (nonatomic, readonly) NSManagedObjectContext* managedObjectContext;
@@ -41,6 +52,12 @@ extern NSString* const RKManagedObjectStoreDidFailSaveNotification;
*/
- (id)initWithStoreFilename:(NSString*)storeFilename;
/**
* Initialize a new managed object store with a SQLite database with the filename specified. If no
* database is found to exist, copy a seed database from the app resource bundle.
*/
- (id)initWithStoreFilename:(NSString *)storeFilename usingSeedDatabase:(NSString*)seedDatabase;
/**
* Save the current contents of the managed object store
*/

View File

@@ -26,7 +26,7 @@ static NSString* const kRKManagedObjectContextKey = @"RKManagedObjectContext";
@synthesize managedObjectCache = _managedObjectCache;
- (id)initWithStoreFilename:(NSString*)storeFilename {
if (self = [self init]) {
if ((self = [self init])) {
_storeFilename = [storeFilename retain];
_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
[self createPersistentStoreCoordinator];
@@ -35,6 +35,29 @@ static NSString* const kRKManagedObjectContextKey = @"RKManagedObjectContext";
return self;
}
- (id)initWithStoreFilename:(NSString *)storeFilename usingSeedDatabase:(NSString*)seedDatabase {
NSError* error = nil;
if ((self = [self init])) {
_storeFilename = [storeFilename retain];
if (NO == [[NSFileManager defaultManager] fileExistsAtPath:self.pathToStoreFile]) {
NSString* seedDatabasePath = [[NSBundle mainBundle] pathForResource:seedDatabase ofType:nil];
NSAssert1(seedDatabasePath, @"Unable to find seed database file '%@' in the Main Bundle, aborting...", seedDatabase);
NSLog(@"No existing database found, copying from seed path '%@'", seedDatabasePath);
[[NSFileManager defaultManager] copyItemAtPath:seedDatabasePath toPath:self.pathToStoreFile error:&error];
if (error) {
NSLog(@"Encountered an error during seed database copy: %@", [error localizedDescription]);
}
NSAssert1([[NSFileManager defaultManager] fileExistsAtPath:seedDatabasePath], @"Seed database not found at path '%@'!", seedDatabasePath);
}
_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
[self createPersistentStoreCoordinator];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_storeFilename release];
@@ -90,8 +113,12 @@ static NSString* const kRKManagedObjectContextKey = @"RKManagedObjectContext";
return managedObjectContext;
}
- (NSString*)pathToStoreFile {
return [[self applicationDocumentsDirectory] stringByAppendingPathComponent:self.storeFilename];
}
- (void)createPersistentStoreCoordinator {
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent:_storeFilename]];
NSURL *storeUrl = [NSURL fileURLWithPath:self.pathToStoreFile];
NSError *error;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:_managedObjectModel];
@@ -107,7 +134,7 @@ static NSString* const kRKManagedObjectContextKey = @"RKManagedObjectContext";
}
- (void)deletePersistantStore {
NSURL* storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent:_storeFilename]];
NSURL* storeUrl = [NSURL fileURLWithPath:self.pathToStoreFile];
NSError* error = nil;
NSLog(@"Error removing persistant store: %@", [error localizedDescription]);
if (error) {

View File

@@ -9,6 +9,8 @@
#import "../ObjectMapping/ObjectMapping.h"
// TODO: This class needs an API scrubbing
// TODO: Should be updated with ability to auto-detect MIME type
// from the file extension. Does this need a delegate property?
@interface RKObjectSeeder : NSObject {
RKObjectManager* _manager;
}

View File

@@ -12,7 +12,8 @@
@implementation RKObjectSeeder
- (id)initWithObjectManager:(RKObjectManager*)manager {
if (self = [self init]) {
self = [self init];
if (self) {
_manager = [manager retain];
}