forked from magicalpanda/MagicalRecord
/
NSPersistentStoreCoordinator+MagicalRecord.m
237 lines (189 loc) · 8.69 KB
/
NSPersistentStoreCoordinator+MagicalRecord.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
// NSPersistentStoreCoordinator+MagicalRecord.m
//
// Created by Saul Mora on 3/11/10.
// Copyright 2010 Magical Panda Software, LLC All rights reserved.
//
#import "CoreData+MagicalRecord.h"
static NSPersistentStoreCoordinator *defaultCoordinator_ = nil;
NSString * const kMagicalRecordPSCDidCompleteiCloudSetupNotification = @"kMagicalRecordPSCDidCompleteiCloudSetupNotification";
@interface NSDictionary (Merging)
- (NSMutableDictionary*) MR_dictionaryByMergingDictionary:(NSDictionary*)d;
@end
@implementation NSPersistentStoreCoordinator (MagicalRecord)
+ (NSPersistentStoreCoordinator *) MR_defaultStoreCoordinator
{
if (defaultCoordinator_ == nil && [MagicalRecordHelpers shouldAutoCreateDefaultPersistentStoreCoordinator])
{
defaultCoordinator_ = [self MR_newPersistentStoreCoordinator];
}
return defaultCoordinator_;
}
+ (void) MR_setDefaultStoreCoordinator:(NSPersistentStoreCoordinator *)coordinator
{
MR_RETAIN(coordinator);
MR_RELEASE(defaultCoordinator_);
defaultCoordinator_ = coordinator;
if (defaultCoordinator_ != nil)
{
NSArray *persistentStores = [defaultCoordinator_ persistentStores];
if ([persistentStores count] && [NSPersistentStore MR_defaultPersistentStore] == nil)
{
[NSPersistentStore MR_setDefaultPersistentStore:[persistentStores objectAtIndex:0]];
}
}
}
- (void) MR_createPathToStoreFileIfNeccessary:(NSURL *)urlForStore
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *pathToStore = [urlForStore URLByDeletingLastPathComponent];
NSError *error = nil;
BOOL pathWasCreated = [fileManager createDirectoryAtPath:[pathToStore path] withIntermediateDirectories:YES attributes:nil error:&error];
if (!pathWasCreated)
{
[MagicalRecordHelpers handleErrors:error];
}
}
- (void) MR_addSqliteStoreNamed:(id)storeFileName withOptions:(__autoreleasing NSDictionary *)options
{
NSURL *url = [storeFileName isKindOfClass:[NSURL class]] ? storeFileName : [NSPersistentStore MR_urlForStoreName:storeFileName];
NSError *error = nil;
[self MR_createPathToStoreFileIfNeccessary:url];
NSPersistentStore *store = [self addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:url
options:options
error:&error];
if (!store)
{
[MagicalRecordHelpers handleErrors:error];
}
}
#pragma mark - Public Instance Methods
- (NSPersistentStore *) MR_addInMemoryStore
{
NSError *error = nil;
NSPersistentStore *store = [self addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:nil
error:&error];
if (!store)
{
[MagicalRecordHelpers handleErrors:error];
}
return store;
}
+ (NSDictionary *) MR_autoMigrationOptions;
{
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
nil];
return options;
}
- (void) MR_addAutoMigratingSqliteStoreNamed:(NSString *) storeFileName
{
NSDictionary *options = [[self class] MR_autoMigrationOptions];
[self MR_addSqliteStoreNamed:storeFileName withOptions:options];
}
#pragma mark - Public Class Methods
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithAutoMigratingSqliteStoreNamed:(NSString *) storeFileName
{
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[coordinator MR_addAutoMigratingSqliteStoreNamed:storeFileName];
//HACK: lame solution to fix automigration error "Migration failed after first pass"
if ([[coordinator persistentStores] count] == 0)
{
[coordinator performSelector:@selector(MR_addAutoMigratingSqliteStoreNamed:) withObject:storeFileName afterDelay:0.5];
}
MR_AUTORELEASE(coordinator);
return coordinator;
}
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithInMemoryStore
{
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[psc MR_addInMemoryStore];
MR_AUTORELEASE(coordinator);
return psc;
}
+ (NSPersistentStoreCoordinator *) MR_newPersistentStoreCoordinator
{
NSPersistentStoreCoordinator *coordinator = [self MR_coordinatorWithSqliteStoreNamed:[MagicalRecordHelpers defaultStoreName]];
MR_RETAIN(coordinator);
return coordinator;
}
- (void) MR_addiCloudContainerID:(NSString *)containerID contentNameKey:(NSString *)contentNameKey localStoreNamed:(NSString *)localStoreName cloudStorePathComponent:(NSString *)subPathComponent;
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *cloudURL = [NSPersistentStore MR_cloudURLForUbiqutiousContainer:containerID];
if (subPathComponent)
{
cloudURL = [cloudURL URLByAppendingPathComponent:subPathComponent];
}
NSDictionary *options = [[self class] MR_autoMigrationOptions];
if (cloudURL) //iCloud is available
{
NSDictionary *iCloudOptions = [NSDictionary dictionaryWithObjectsAndKeys:
contentNameKey, NSPersistentStoreUbiquitousContentNameKey,
cloudURL, NSPersistentStoreUbiquitousContentURLKey, nil];
options = [options MR_dictionaryByMergingDictionary:iCloudOptions];
}
else
{
MRLog(@"iCloud is not enabled");
}
[self lock];
[self MR_addSqliteStoreNamed:localStoreName withOptions:options];
[self unlock];
dispatch_async(dispatch_get_main_queue(), ^{
MRLog(@"iCloud Store Enabled: %@", [MagicalRecordHelpers currentStack]);
[[NSNotificationCenter defaultCenter] postNotificationName:kMagicalRecordPSCDidCompleteiCloudSetupNotification object:nil];
});
});
}
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithiCloudContainerID:(NSString *)containerID
contentNameKey:(NSString *)contentNameKey
localStoreNamed:(NSString *)localStoreName
cloudStorePathComponent:(NSString *)subPathComponent;
{
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[psc MR_addiCloudContainerID:containerID
contentNameKey:contentNameKey
localStoreNamed:localStoreName
cloudStorePathComponent:subPathComponent];
MR_AUTORELEASE(psc);
return psc;
}
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithPersitentStore:(NSPersistentStore *)persistentStore;
{
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[psc MR_addSqliteStoreNamed:[persistentStore URL] withOptions:nil];
MR_AUTORELEASE(psc);
return psc;
}
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName withOptions:(NSDictionary *)options
{
NSManagedObjectModel *model = [NSManagedObjectModel MR_defaultManagedObjectModel];
NSPersistentStoreCoordinator *psc = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:model];
[psc MR_addSqliteStoreNamed:storeFileName withOptions:options];
MR_AUTORELEASE(psc);
return psc;
}
+ (NSPersistentStoreCoordinator *) MR_coordinatorWithSqliteStoreNamed:(NSString *)storeFileName
{
return [self MR_coordinatorWithSqliteStoreNamed:storeFileName withOptions:nil];
}
@end
@implementation NSDictionary (Merging)
- (NSMutableDictionary *) MR_dictionaryByMergingDictionary:(NSDictionary *)d;
{
NSMutableDictionary *mutDict = [self mutableCopy];
[mutDict addEntriesFromDictionary:d];
return mutDict;
}
@end