diff --git a/ANDYDataManager.podspec b/ANDYDataManager.podspec index 013f801..92a0941 100644 --- a/ANDYDataManager.podspec +++ b/ANDYDataManager.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "ANDYDataManager" - s.version = "1.1" + s.version = "1.2" s.summary = "CoreData stack set up boilerplate." s.description = <<-DESC * Feeling tired of having CoreData boilerplate in your AppDelegate? diff --git a/ANDYDataManager/ANDYDataManager.h b/ANDYDataManager/ANDYDataManager.h index 0b0b05d..037bffe 100644 --- a/ANDYDataManager/ANDYDataManager.h +++ b/ANDYDataManager/ANDYDataManager.h @@ -9,12 +9,8 @@ @import Foundation; @import CoreData; -@protocol ANDYDataManagerDataSource; - @interface ANDYDataManager : NSObject -@property (nonatomic, weak) id dataSource; - /*! * Provides a NSManagedObjectContext appropriate for use on the main * thread. @@ -46,25 +42,23 @@ + (void)setUpStackWithInMemoryStore; /*! - * Saves current state of mainContext into the database. + * Sets the model name in case it's different from the bundle name */ -- (void)persistContext; ++ (void)setModelName:(NSString *)modelName; /*! - * Destroys state of ANDYDataManager. + * Sets the model bundle in case it's different from the main bundle */ -- (void)destroy; - -@end ++ (void)setModelBundle:(NSBundle *)modelBundle; -@protocol ANDYDataManagerDataSource - -@optional +/*! + * Saves current state of mainContext into the database. + */ +- (void)persistContext; /*! - * Optional protocol to use a custom managed object model. - * Default value uses the app name. + * Destroys state of ANDYDataManager. */ -- (NSString *)managedObjectModelName; +- (void)destroy; @end diff --git a/ANDYDataManager/ANDYDataManager.m b/ANDYDataManager/ANDYDataManager.m index 50374cb..598658e 100644 --- a/ANDYDataManager/ANDYDataManager.m +++ b/ANDYDataManager/ANDYDataManager.m @@ -16,7 +16,10 @@ @interface ANDYDataManager () @property (strong, nonatomic) NSManagedObjectContext *writerContext; @property (strong, nonatomic) NSManagedObjectModel *managedObjectModel; @property (strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; + @property (nonatomic) BOOL inMemoryStore; +@property (nonatomic, copy) NSString *modelName; +@property (nonatomic, strong) NSBundle *modelBundle; @end @@ -27,6 +30,16 @@ + (void)setUpStackWithInMemoryStore [[self sharedManager] setInMemoryStore:YES]; } ++ (void)setModelName:(NSString *)modelName +{ + [[self sharedManager] setModelName:modelName]; +} + ++ (void)setModelBundle:(NSBundle *)modelBundle +{ + [[self sharedManager] setModelBundle:modelBundle]; +} + + (ANDYDataManager *)sharedManager { static ANDYDataManager *__sharedInstance; @@ -38,6 +51,18 @@ + (ANDYDataManager *)sharedManager return __sharedInstance; } +- (NSString *)modelName +{ + if (_modelName) return _modelName; + + NSBundle *bundle = (self.modelBundle) ?: [NSBundle mainBundle]; + + NSString *string = [[bundle infoDictionary] objectForKey:@"CFBundleName"]; + _modelName = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; + + return _modelName; +} + - (void)setUpSaveNotificationForContext:(NSManagedObjectContext *)context { [[NSNotificationCenter defaultCenter] addObserverForName:NSManagedObjectContextDidSaveNotification @@ -145,9 +170,12 @@ - (NSManagedObjectModel *)managedObjectModel { if (_managedObjectModel) return _managedObjectModel; - NSURL *modelURL = [[NSBundle mainBundle] URLForResource:[self appName] withExtension:@"momd"]; + NSBundle *bundle = (self.modelBundle) ?: [NSBundle mainBundle]; + NSURL *modelURL = [bundle URLForResource:self.modelName withExtension:@"momd"]; _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; + _managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:[NSBundle allBundles]]; + return _managedObjectModel; } @@ -157,7 +185,7 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator NSURL *storeURL = nil; - NSString *filePath = [NSString stringWithFormat:@"%@.sqlite", [self appName]]; + NSString *filePath = [NSString stringWithFormat:@"%@.sqlite", self.modelName]; storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:filePath]; NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @YES, NSInferMappingModelAutomaticallyOption: @YES}; @@ -206,17 +234,6 @@ - (NSURL *)applicationDocumentsDirectory inDomains:NSUserDomainMask] lastObject]; } -- (NSString *)appName -{ - if ([self.dataSource respondsToSelector:@selector(managedObjectModelName)]) { - return [self.dataSource managedObjectModelName]; - } - - NSString *string = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"]; - NSString *trimmedString = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; - return trimmedString; -} - #pragma mark - Class methods + (void)performInBackgroundContext:(void (^)(NSManagedObjectContext *context))operation @@ -235,7 +252,7 @@ + (NSManagedObjectContext *)backgroundContext context.persistentStoreCoordinator = [[self sharedManager] persistentStoreCoordinator]; context.undoManager = nil; context.mergePolicy = NSOverwriteMergePolicy; - + [[NSNotificationCenter defaultCenter] addObserver:[self sharedManager] selector:@selector(backgroundThreadDidSave:) name:NSManagedObjectContextDidSaveNotification diff --git a/Demo/Demo/Controllers/ANDYMainTableViewController.m b/Demo/Demo/Controllers/ANDYMainTableViewController.m index a5c8192..f9e015b 100644 --- a/Demo/Demo/Controllers/ANDYMainTableViewController.m +++ b/Demo/Demo/Controllers/ANDYMainTableViewController.m @@ -76,13 +76,11 @@ - (void)createTask - (void)createAlternativeTask { - NSManagedObjectContext *context = [ANDYDataManager backgroundContext]; - [context performBlock:^{ - Task *task = [Task insertInManagedObjectContext:context]; - task.title = @"Hello!"; - task.date = [NSDate date]; - [context save:nil]; - }]; + NSManagedObjectContext *context = [[ANDYDataManager sharedManager] mainContext]; + Task *task = [Task insertInManagedObjectContext:context]; + task.title = @"Hello MAIN!"; + task.date = [NSDate date]; + [context save:nil]; } -@end \ No newline at end of file +@end