RestKit/RestKit — Issue #566: Refactoring RKManagedObjectStore / NSManagedObjectModel
Author: grgcombs
Created: 2012-02-24 04:00:06+00:00
State: open
Labels: Core Data

The way RKManagedObjectStore combines the various NSManagedObjectModels leads to a bit of a mess in the initialization method.

The way I'm cooking up passing in some dynamic MOMs leads to a messier series of mergedModelFromBundles and modelByMergingModels, and it makes for some unpleasant debugging when things go bad. I refactored a little and added a different designated initializer that streamlines its own code, while cleaning up the model merging process.

Let me know what you think:

```objc

+ (NSManagedObjectModel *)combineObjectModels:(NSArray *)customModels withAllBundles:(BOOL)usesAllBundles withSearching:(BOOL)usesSearch {
NSMutableArray* allManagedObjectModels = [[NSMutableArray alloc] init];
if (usesAllBundles) {
[allManagedObjectModels addObject:[NSManagedObjectModel mergedModelFromBundles:[NSBundle allBundles]]];
}
if (usesSearch) {
NSString* rkBundlePath = [[NSBundle mainBundle] pathForResource:@"RestKitResources" ofType:@"bundle"];
NSURL* rkCoreDataLibraryMOMURL = [[NSBundle bundleWithPath:rkBundlePath] URLForResource:@"RestKitCoreData" withExtension:@"momd"];
NSManagedObjectModel* rkCoreDataLibraryMOM = [[NSManagedObjectModel alloc] initWithContentsOfURL:rkCoreDataLibraryMOMURL];
if (rkCoreDataLibraryMOM) {
[allManagedObjectModels addObject:rkCoreDataLibraryMOM];
[rkCoreDataLibraryMOM release];
} else {
RKLogWarning(@"Unable to find RestKitCoreData.momd within the RestKitCoreDataBundle.bundle");
}
}
if (customModels) {
[allManagedObjectModels addObjectsFromArray:customModels];
}
NSManagedObjectModel *combinedModel = [NSManagedObjectModel modelByMergingModels:allManagedObjectModels];
[allManagedObjectModels release];
return combinedModel;
}

- (id)initWithStoreFilename:(NSString *)storeFilename inDirectory:(NSString *)nilOrDirectoryPath usingSeedDatabaseName:(NSString *)nilOrNameOfSeedDatabaseInMainBundle combinedObjectModel:(NSManagedObjectModel*)combinedObjectModel delegate:(id)delegate {
self = [self init];
if (self) {

_storeFilename = [storeFilename retain];

if (nilOrDirectoryPath == nil) {
nilOrDirectoryPath = [RKDirectory applicationDataDirectory];
} else {
BOOL isDir;
NSAssert1([[NSFileManager defaultManager] fileExistsAtPath:nilOrDirectoryPath isDirectory:&isDir] && isDir == YES, @"Specified storage directory exists", nilOrDirectoryPath);
}
_pathToStoreFile = [[nilOrDirectoryPath stringByAppendingPathComponent:_storeFilename] retain];

NSParameterAssert(combinedObjectModel != NULL);
_managedObjectModel = [combinedObjectModel retain];

if (nilOrNameOfSeedDatabaseInMainBundle) {
[self createStoreIfNecessaryUsingSeedDatabase:nilOrNameOfSeedDatabaseInMainBundle];
}

_delegate = delegate;

[self createPersistentStoreCoordinator];

[RKSearchWordObserver sharedObserver];
}

return self;
}

- (id)initWithStoreFilename:(NSString *)storeFilename inDirectory:(NSString *)nilOrDirectoryPath usingSeedDatabaseName:(NSString *)nilOrNameOfSeedDatabaseInMainBundle managedObjectModel:(NSManagedObjectModel*)nilOrManagedObjectModel delegate:(id)delegate {

NSArray *customModels = nil;
if (nilOrManagedObjectModel) {
customModels = [NSArray arrayWithObject:nilOrManagedObjectModel];
}
NSManagedObjectModel *combinedModel = [RKManagedObjectStore combineObjectModels:customModels withAllBundles:YES withSearching:YES];
self = [self initWithStoreFilename:storeFilename inDirectory:nilOrDirectoryPath usingSeedDatabaseName:nilOrNameOfSeedDatabaseInMainBundle combinedObjectModel:combinedModel delegate:delegate];

return self;
}
```