5
5
#import " NSString+LocalizeSSY.h"
6
6
#import " NSError+InfoAccess.h"
7
7
#import " NSBundle+MainApp.h"
8
- #import " SSYPersistentDocumentMultiMigrator.h"
9
8
#import " NSManagedObjectContext+Cheats.h"
10
9
#import " NSError+DecodeCodes.h"
11
10
#import " NSObject+MoreDescriptions.h"
17
16
/* BSManagedDocument is a open source replacement for NSPersistentDocument.
18
17
It is recommended for any Core Data document-based app.
19
18
https://github.com/jerrykrinock/BSManagedDocument
20
- */
19
+
20
+ Objective-C declaration for SSYMOCManager+Swift.h, which must be compiled
21
+ in your target. The normal, easier way to do this is simply
22
+ #import "YourTarget-Swift.h"
23
+ which refers to the header automatically generated by Xcode.
24
+ But we don't know what YourTarget is. */
25
+ @interface SSYMOCManager (Swift)
26
+ + (BOOL )migrateIfNeededWithUrl : (NSURL *)url
27
+ momdName : (NSString *)momdName
28
+ error : (NSError **)error_p ;
29
+ @end
21
30
22
31
static NSManagedObjectContext * static_pasteboardManagedObjectContext = nil ;
23
32
@@ -180,35 +189,35 @@ + (void)removeStoreAtUrl:(NSURL*)url
180
189
}
181
190
182
191
+ (NSPersistentStoreCoordinator *)persistentStoreCoordinatorType : (NSString *)storeType
183
- identifier : (NSString *)identifier
184
- momdName : (NSString *)momdName
192
+ identifier : (NSString *)identifier
193
+ momdName : (NSString *)momdName
185
194
options : (NSDictionary *)options
186
195
nukeAndPaveIfCorrupt : (BOOL )nukeAndPaveIfCorrupt
187
- error_p : (NSError **)error_p {
188
- NSPersistentStore * persistentStore = nil ;
189
-
190
- NSArray * bundles = [NSArray arrayWithObject: [NSBundle mainAppBundle ]] ;
191
- NSManagedObjectModel * mergedMOM = [NSManagedObjectModel mergedModelFromBundles: bundles] ;
192
- NSPersistentStoreCoordinator * newPSC = [[NSPersistentStoreCoordinator alloc ] initWithManagedObjectModel: mergedMOM] ;
193
-
194
- if ([storeType isEqualToString: NSSQLiteStoreType ]) {
195
- NSURL * url = [self sqliteStoreURLWithIdentifier: identifier] ;
196
- // i.e file://localhost/Users/jk/Library/Application%20Support/BookMacster/BookMacster.sql
197
-
198
- NSFileManager * fm = [NSFileManager defaultManager ] ;
199
- BOOL ok = YES ;
200
-
201
- // An undocumented fact about addPersistentStoreWithType:configuration:URL:options:error:
202
- // is that if the parent folder does not exist, the method will fail to create a
203
- // persistent store with no explanation. So we make sure it exists
204
- NSString * parentPath = [[url path ] stringByDeletingLastPathComponent ] ;
205
-
196
+ error_p : (NSError **)error_p {
197
+ NSPersistentStore * persistentStore = nil ;
198
+
199
+ NSArray * bundles = [NSArray arrayWithObject: [NSBundle mainAppBundle ]] ;
200
+ NSManagedObjectModel * mergedMOM = [NSManagedObjectModel mergedModelFromBundles: bundles] ;
201
+ NSPersistentStoreCoordinator * newPSC = [[NSPersistentStoreCoordinator alloc ] initWithManagedObjectModel: mergedMOM] ;
202
+
203
+ if ([storeType isEqualToString: NSSQLiteStoreType ]) {
204
+ NSURL * url = [self sqliteStoreURLWithIdentifier: identifier] ;
205
+ // i.e file://localhost/Users/jk/Library/Application%20Support/BookMacster/BookMacster.sql
206
+
207
+ NSFileManager * fm = [NSFileManager defaultManager ] ;
208
+ BOOL ok = YES ;
209
+
210
+ // An undocumented fact about addPersistentStoreWithType:configuration:URL:options:error:
211
+ // is that if the parent folder does not exist, the method will fail to create a
212
+ // persistent store with no explanation. So we make sure it exists
213
+ NSString * parentPath = [[url path ] stringByDeletingLastPathComponent ] ;
214
+
206
215
if (!parentPath) {
207
216
ok = NO ;
208
217
}
209
218
210
- BOOL isDirectory = NO ;
211
- BOOL fileExists = NO ;
219
+ BOOL isDirectory = NO ;
220
+ BOOL fileExists = NO ;
212
221
213
222
if (ok) {
214
223
[fm fileExistsAtPath: parentPath isDirectory: &isDirectory] ;
@@ -218,50 +227,32 @@ + (NSPersistentStoreCoordinator*)persistentStoreCoordinatorType:(NSString*)store
218
227
error: error_p] ;
219
228
}
220
229
}
221
-
222
- NSError * error = nil ;
223
- if (ok && ((fileExists && !isDirectory) || !fileExists)) {
224
- // Create parent directory
225
- ok = [fm createDirectoryAtPath: parentPath
226
- withIntermediateDirectories: YES
227
- attributes: nil
228
- error: &error] ;
229
- }
230
-
231
- if (!ok) {
232
- NSString * msg = [NSString stringWithFormat:
233
- @" Could not create directory at %@ " ,
234
- parentPath] ;
230
+
231
+ NSError * error = nil ;
232
+ if (ok && ((fileExists && !isDirectory) || !fileExists)) {
233
+ // Create parent directory
234
+ ok = [fm createDirectoryAtPath: parentPath
235
+ withIntermediateDirectories: YES
236
+ attributes: nil
237
+ error: &error] ;
238
+ }
239
+
240
+ if (!ok) {
241
+ NSString * msg = [NSString stringWithFormat:
242
+ @" Could not create directory at %@ " ,
243
+ parentPath] ;
235
244
error = [SSYMakeError (95745 , msg) errorByAddingUnderlyingError: error] ;
236
245
NSLog (@" %@ " , error) ;
237
- if (error_p) {
238
- *error_p = error ;
246
+ if (error_p) {
247
+ *error_p = error ;
239
248
}
240
- }
241
-
242
- if (ok) {
243
- if (momdName) {
244
- // Using Multi-Hop Migration
245
- ok = [SSYPersistentDocumentMultiMigrator migrateIfNeededStoreAtUrl: url
246
- storeOptions: options
247
- storeType: NSSQLiteStoreType
248
- momdName: momdName
249
- document: nil
250
- error_p: error_p] ;
251
- }
252
- else {
253
- // Using Core Data's built-in Single-Hop Migration only
254
- NSDictionary * moreOption = [NSDictionary dictionaryWithObjectsAndKeys:
255
- [NSNumber numberWithBool: YES ], NSMigratePersistentStoresAutomaticallyOption ,
256
- nil ] ;
257
- if (options) {
258
- options = [options dictionaryByAddingEntriesFromDictionary: moreOption] ;
259
- }
260
- else {
261
- options = moreOption ;
262
- }
263
- }
264
-
249
+ }
250
+
251
+ if (ok) {
252
+ ok = [self migrateIfNeededWithUrl: url
253
+ momdName: momdName
254
+ error: &error];
255
+
265
256
if (ok) {
266
257
// Add persistent store to it
267
258
persistentStore = [newPSC addPersistentStoreWithType: NSSQLiteStoreType
@@ -280,76 +271,71 @@ + (NSPersistentStoreCoordinator*)persistentStoreCoordinatorType:(NSString*)store
280
271
didSimulateBadStoreOnce = YES ;
281
272
}
282
273
#endif
283
- if (!persistentStore) {
284
- BOOL fileExists = [fm fileExistsAtPath: [url path ]] ;
285
- if (fileExists) {
286
- if (nukeAndPaveIfCorrupt) {
287
- // If we did not get a store but file exists, must be a corrupt file or store.
288
- [self removeStoreAtUrl: url
289
- tildefy: NO ];
290
- persistentStore = [newPSC addPersistentStoreWithType: NSSQLiteStoreType
291
- configuration: nil
292
- URL: url
293
- options: options
294
- error: error_p] ;
295
- }
296
-
297
- if (!persistentStore) {
298
- if (error_p) {
299
- NSString * msg = [NSString stringWithFormat: @" Click 'Move' to move the unreadable database\n %@ \n to your desktop and start a new database. The item properties in your old database will not be available to %@ ." ,
300
- [url path ],
301
- [[NSBundle mainAppBundle ] objectForInfoDictionaryKey: @" CFBundleExecutable" ]] ;
302
- // We used CFBundleExecutable instead of CFBundleName to get an unlocalized app name.
303
- *error_p = [*error_p errorByAddingLocalizedRecoverySuggestion: msg] ;
304
- NSArray * recoveryOptions = [NSArray arrayWithObjects:
305
- [NSString localize: @" move" ],
306
- [NSString localize: @" cancel" ],
307
- nil ] ;
308
- *error_p = [*error_p errorByAddingLocalizedRecoveryOptions: recoveryOptions] ;
309
- *error_p = [*error_p errorByAddingRecoveryAttempter: [self sharedMOCManager ]] ;
310
- *error_p = [*error_p errorByAddingUserInfoObject: url
311
- forKey: constKeyStoreUrl] ;
312
- }
313
- }
314
- }
315
- else {
316
- NSString * msg = [NSString stringWithFormat:
317
- @" Could not create persistent store file at path %@ " ,
318
- [url absoluteString ]] ;
319
- NSLog (@" %@ " , msg) ;
320
- if (error_p) {
321
- *error_p = SSYMakeError (51298 , msg) ;
322
- }
323
- }
324
- }
325
- }
326
- else if ([*error_p involvesCode: SSYPersistentDocumentMultiMigratorErrorCodeNoSourceModel
327
- domain: SSYPersistentDocumentMultiMigratorErrorDomain]) {
328
- [[self class ] removeStoreAtUrl: url
329
- tildefy: YES ] ;
330
- }
331
- }
332
- }
333
- else if ([storeType isEqualToString: NSInMemoryStoreType ]) {
334
- persistentStore = [newPSC addPersistentStoreWithType: NSInMemoryStoreType
335
- configuration: nil
336
- URL: nil
337
- options: options
338
- error: error_p] ;
339
-
340
- if (!persistentStore) {
341
- NSLog (@" Internal Error 535-1498. Failed to create inMemory persistent store" ) ;
342
- }
343
- }
344
-
345
- if (!persistentStore) {
346
- // If persistentStore could not be added, we don't want the
347
- // newPSC to be returned because it won't work
348
- [newPSC release ] ;
349
- newPSC = nil ;
350
- }
351
-
352
- return [newPSC autorelease ] ;
274
+ if (!persistentStore) {
275
+ BOOL fileExists = [fm fileExistsAtPath: [url path ]] ;
276
+ if (fileExists) {
277
+ if (nukeAndPaveIfCorrupt) {
278
+ // If we did not get a store but file exists, must be a corrupt file or store.
279
+ [self removeStoreAtUrl: url
280
+ tildefy: NO ];
281
+ persistentStore = [newPSC addPersistentStoreWithType: NSSQLiteStoreType
282
+ configuration: nil
283
+ URL: url
284
+ options: options
285
+ error: error_p] ;
286
+ }
287
+
288
+ if (!persistentStore) {
289
+ if (error_p) {
290
+ NSString * msg = [NSString stringWithFormat: @" Click 'Move' to move the unreadable database\n %@ \n to your desktop and start a new database. The item properties in your old database will not be available to %@ ." ,
291
+ [url path ],
292
+ [[NSBundle mainAppBundle ] objectForInfoDictionaryKey: @" CFBundleExecutable" ]] ;
293
+ // We used CFBundleExecutable instead of CFBundleName to get an unlocalized app name.
294
+ *error_p = [*error_p errorByAddingLocalizedRecoverySuggestion: msg] ;
295
+ NSArray * recoveryOptions = [NSArray arrayWithObjects:
296
+ [NSString localize: @" move" ],
297
+ [NSString localize: @" cancel" ],
298
+ nil ] ;
299
+ *error_p = [*error_p errorByAddingLocalizedRecoveryOptions: recoveryOptions] ;
300
+ *error_p = [*error_p errorByAddingRecoveryAttempter: [self sharedMOCManager ]] ;
301
+ *error_p = [*error_p errorByAddingUserInfoObject: url
302
+ forKey: constKeyStoreUrl] ;
303
+ }
304
+ }
305
+ }
306
+ else {
307
+ NSString * msg = [NSString stringWithFormat:
308
+ @" Could not create persistent store file at path %@ " ,
309
+ [url absoluteString ]] ;
310
+ NSLog (@" %@ " , msg) ;
311
+ if (error_p) {
312
+ *error_p = SSYMakeError (51298 , msg) ;
313
+ }
314
+ }
315
+ }
316
+ }
317
+ }
318
+ }
319
+ else if ([storeType isEqualToString: NSInMemoryStoreType ]) {
320
+ persistentStore = [newPSC addPersistentStoreWithType: NSInMemoryStoreType
321
+ configuration: nil
322
+ URL: nil
323
+ options: options
324
+ error: error_p] ;
325
+
326
+ if (!persistentStore) {
327
+ NSLog (@" Internal Error 535-1498. Failed to create inMemory persistent store" ) ;
328
+ }
329
+ }
330
+
331
+ if (!persistentStore) {
332
+ // If persistentStore could not be added, we don't want the
333
+ // newPSC to be returned because it won't work
334
+ [newPSC release ] ;
335
+ newPSC = nil ;
336
+ }
337
+
338
+ return [newPSC autorelease ] ;
353
339
}
354
340
355
341
- (NSManagedObjectContext *)managedObjectContextType : (NSString *)type
0 commit comments