-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSPersistentStoreCoordinator+Custom.m
62 lines (47 loc) · 2.38 KB
/
NSPersistentStoreCoordinator+Custom.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// NSPersistentStoreCoordinator+Custom.m
// CocoaPodsManager
//
// Created by Andrei Zaharia on 9/18/13.
// Copyright (c) 2013 Andy. All rights reserved.
//
#import "NSPersistentStoreCoordinator+Custom.h"
@implementation NSPersistentStoreCoordinator (Custom)
static NSPersistentStoreCoordinator *_sharedPersistentStore = nil;
static NSString *_dataModelName = nil;
static NSString *_storeFileName = nil;
+ (NSString *)applicationDocumentsDirectory {
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}
+ (void) setDataModelName: (NSString *) name withStoreName: (NSString *) storeFileName {
_dataModelName = name;
_storeFileName = storeFileName;
}
+(NSPersistentStoreCoordinator *) sharedPersisntentStoreCoordinator
{
NSAssert(_dataModelName, @"Core Data model name has not been set. Use [NSPersistentStoreCoordinator setDataModelName:].");
if (!_sharedPersistentStore) {
NSString *storePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent: _storeFileName];
NSURL *storeUrl = [NSURL fileURLWithPath:storePath];
NSBundle *bundle = [NSBundle mainBundle];
NSString *resourcePath = [bundle resourcePath];
NSString *modelFileName = [_dataModelName stringByAppendingPathExtension:@"momd"];
NSString *modelPath = [resourcePath stringByAppendingPathComponent: modelFileName];
NSURL *modelUrl = [NSURL fileURLWithPath: modelPath];
NSManagedObjectModel *_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL: modelUrl];
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption: @(YES),
NSInferMappingModelAutomaticallyOption : @(YES)};
NSError *error;
_sharedPersistentStore = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: _managedObjectModel];
if (![_sharedPersistentStore addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
return _sharedPersistentStore;
}
+ (void) setNewPresistentStore: (NSPersistentStoreCoordinator *) store
{
_sharedPersistentStore = store;
}
@end