Skip to content
This repository has been archived by the owner on Nov 24, 2021. It is now read-only.

Improve naming and bundle setup #2

Merged
merged 1 commit into from
Nov 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ANDYDataManager.podspec
Original file line number Diff line number Diff line change
@@ -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?
Expand Down
26 changes: 10 additions & 16 deletions ANDYDataManager/ANDYDataManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
@import Foundation;
@import CoreData;

@protocol ANDYDataManagerDataSource;

@interface ANDYDataManager : NSObject

@property (nonatomic, weak) id <ANDYDataManagerDataSource> dataSource;

/*!
* Provides a NSManagedObjectContext appropriate for use on the main
* thread.
Expand Down Expand Up @@ -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 <NSObject>

@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
45 changes: 31 additions & 14 deletions ANDYDataManager/ANDYDataManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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};
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 6 additions & 8 deletions Demo/Demo/Controllers/ANDYMainTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
@end