forked from magicalpanda/MagicalRecord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSPersistentStoreCoordinator+ActiveRecord.m
129 lines (106 loc) · 4.77 KB
/
NSPersistentStoreCoordinator+ActiveRecord.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//
// NSPersistentStoreCoordinator+ActiveRecord.m
//
// Created by Saul Mora on 3/11/10.
// Copyright 2010 Magical Panda Software, LLC All rights reserved.
//
#import "NSPersistentStoreCoordinator+ActiveRecord.h"
#import "NSManagedObjectModel+ActiveRecord.h"
#import "NSPersistentStore+ActiveRecord.h"
static NSPersistentStoreCoordinator *defaultCoordinator = nil;
@implementation NSPersistentStoreCoordinator (ActiveRecord)
+ (NSPersistentStoreCoordinator *) defaultStoreCoordinator
{
@synchronized (self)
{
if (defaultCoordinator == nil)
{
defaultCoordinator = [self newPersistentStoreCoordinator];
}
}
return defaultCoordinator;
}
+ (void) setDefaultStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator
{
[defaultCoordinator release];
defaultCoordinator = [coordinator retain];
}
- (void) setupSqliteStoreNamed:(id)storeFileName withOptions:(NSDictionary *)options
{
NSURL *url = [storeFileName isKindOfClass:[NSURL class]] ? storeFileName : [NSPersistentStore urlForStoreName:storeFileName];
NSError *error = nil;
NSPersistentStore *store = [self addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:url
options:options
error:&error];
if (!store)
{
[ActiveRecordHelpers handleErrors:error];
}
[NSPersistentStore setDefaultPersistentStore:store];
}
+ (NSPersistentStoreCoordinator *) coordinatorWithPersitentStore:(NSPersistentStore *)persistentStore;
{
NSManagedObjectModel *model = [NSManagedObjectModel defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[psc setupSqliteStoreNamed:[persistentStore URL] withOptions:nil];
return [psc autorelease];
}
+ (NSPersistentStoreCoordinator *) coordinatorWithSqliteStoreNamed:(NSString *)storeFileName withOptions:(NSDictionary *)options
{
NSManagedObjectModel *model = [NSManagedObjectModel defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[psc setupSqliteStoreNamed:storeFileName withOptions:options];
return [psc autorelease];
}
+ (NSPersistentStoreCoordinator *) coordinatorWithSqliteStoreNamed:(NSString *)storeFileName
{
return [self coordinatorWithSqliteStoreNamed:storeFileName withOptions:nil];
}
- (void) setupAutoMigratingSqliteStoreNamed:(NSString *) storeFileName
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
[self setupSqliteStoreNamed:storeFileName withOptions:options];
}
+ (NSPersistentStoreCoordinator *) coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *) storeFileName
{
NSManagedObjectModel *model = [NSManagedObjectModel defaultManagedObjectModel];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[coordinator setupAutoMigratingSqliteStoreNamed:storeFileName];
//HACK: lame solution to fix automigration error "Migration failed after first pass"
if ([[coordinator persistentStores] count] == 0)
{
[coordinator performSelector:@selector(setupAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5];
}
return [coordinator autorelease];
}
+ (NSPersistentStoreCoordinator *) coordinatorWithInMemoryStore
{
NSManagedObjectModel *model = [NSManagedObjectModel defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[NSPersistentStore setDefaultPersistentStore:[psc addInMemoryStore]];
return [psc autorelease];
}
- (NSPersistentStore *) addInMemoryStore
{
NSError *error = nil;
NSPersistentStore *store = [self addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:nil
error:&error];
if (!store)
{
[ActiveRecordHelpers handleErrors:error];
}
return store;
}
+ (NSPersistentStoreCoordinator *) newPersistentStoreCoordinator
{
return [[self coordinatorWithSqliteStoreNamed:kActiveRecordDefaultStoreFileName] retain];
}
@end