-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYMOCManager.m
673 lines (575 loc) · 26.1 KB
/
SSYMOCManager.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#import "SSYMOCManager.h"
#import "NSError+Recovery.h"
#import "NSError+MyDomain.h"
#import "NSString+MorePaths.h"
#import "NSString+LocalizeSSY.h"
#import "NSError+InfoAccess.h"
#import "NSBundle+MainApp.h"
#import "NSManagedObjectContext+Cheats.h"
#import "NSError+DecodeCodes.h"
#import "NSObject+MoreDescriptions.h"
#import "NSBundle+SSYMotherApp.h"
#import "NSBundle+MainApp.h"
#import "NSDictionary+SimpleMutations.h"
#import "NSManagedObject+SSYCopying.h"
#import "BSManagedDocument.h"
/* BSManagedDocument is a open source replacement for NSPersistentDocument.
It is recommended for any Core Data document-based app.
https://github.com/jerrykrinock/BSManagedDocument
Objective-C declaration for SSYMOCManager+Swift.h, which must be compiled
in your target. The normal, easier way to do this is simply
#import "YourTarget-Swift.h"
which refers to the header automatically generated by Xcode.
But we don't know what YourTarget is. */
@interface SSYMOCManager (Swift)
+ (BOOL)migrateIfNeededWithUrl:(NSURL*)url
momdName:(NSString*)momdName
error:(NSError**)error_p;
@end
static NSManagedObjectContext* static_pasteboardManagedObjectContext = nil;
NSString* const constKeyMOC = @"moc" ;
NSString* const constKeyOwner = @"owr" ;
NSString* const constKeyStoreUrl = @"sturl" ;
#if 0
#define SIMULATE_BAD_STORE 1
static BOOL didSimulateBadStoreOnce = NO;
#endif
// This is a singleton, but not a "true singletons", because
// I didn't bother to override
// +allocWithZone:
// -copyWithZone:
// -retain
// -retainCount
// -release
// -autorelease
static SSYMOCManager* sharedMOCManager = nil ;
@interface SSYMOCManager (PrivateHeader)
@end
@implementation SSYMOCManager
+ (SSYMOCManager*)sharedMOCManager {
@synchronized(self) {
if (!sharedMOCManager) {
sharedMOCManager = [[self alloc] init] ;
}
}
// No autorelease. This sticks around forever.
return sharedMOCManager ;
}
- (NSMutableDictionary*)inMemoryMOCDics {
if (!inMemoryMOCDics) {
inMemoryMOCDics = [[NSMutableDictionary alloc] init] ;
}
return inMemoryMOCDics ;
}
- (NSMutableDictionary*)sqliteMOCDics {
if (!sqliteMOCDics) {
sqliteMOCDics = [[NSMutableDictionary alloc] init] ;
}
return sqliteMOCDics ;
}
- (NSMutableDictionary*)docMOCDics {
if (!docMOCDics) {
docMOCDics = [[NSMutableDictionary alloc] init] ;
}
return docMOCDics ;
}
- (void)attemptRecoveryFromError:(NSError*)error
recoveryOption:(NSUInteger)recoveryOption
delegate:(id)dontUseThis
didRecoverSelector:(SEL)useInvocationFromInfoDicInstead
contextInfo:(void*)contextInfo {
error = [error deepestRecoverableError] ;
switch(recoveryOption) {
case NSAlertFirstButtonReturn:;
// "Move"
NSURL* url = [[error userInfo] objectForKey:constKeyStoreUrl];
[[self class] removeStoreAtUrl:url
tildefy:YES];
// Since the old corrupted file is gone, +[SSYMOCManager addPersistentStoreWithType:::::]
// will create a new file the next time it is invoked. Problem should be solved.
break ;
}
}
+ (NSString*)directoryOfSqliteMOCs {
NSBundle* mainAppBundle = [NSBundle mainAppBundle] ;
return [mainAppBundle applicationSupportPathForMotherApp] ;
}
+ (NSURL*)sqliteStoreURLWithIdentifier:(NSString*)identifier {
NSString* filename ;
if (!identifier) {
identifier = @"Shared" ;
}
filename = [identifier stringByAppendingPathExtension:SSYManagedObjectContextPathExtensionForSqliteStores] ;
NSString* path = [[self directoryOfSqliteMOCs] stringByAppendingPathComponent:filename] ;
NSURL* url = nil ;
if (path) {
// Above if() was added in BookMacster 1.19 to eliminate crash in case
// the app's Application Suppport folder is trashed while Worker is
// watching one or more files in it for changes.
url = [NSURL fileURLWithPath:path] ;
}
return url ;
}
+ (BOOL)sqliteStoreExistsForIdentifier:(NSString*)identifier {
NSURL* url = [self sqliteStoreURLWithIdentifier:identifier] ;
return [[NSFileManager defaultManager] fileExistsAtPath:[url path]] ;
}
+ (void)removeStoreAtUrl:(NSURL*)url
tildefy:(BOOL)tildefy {
/* In some cases, you may want to keep all tildefied files, adding more
tildes, for example, Foo~~.sql. I have written code which does that
for document files, but we do not use it here. Prior prior tildefied
files are removed. */
/* We ignore any errors produced by File Manager calls in this method,
because it is expected that an indicated file may not always exist, for
various reasons. In particular, if the subject file Foo.sql is really bad,
sometimes Core Data may have already moved it to Foo.unreadable.sql. */
NSString* originalPath = [url path];
NSString* originalShmPath = [originalPath stringByAppendingString:@"-shm"];
NSString* originalWalPath = [originalPath stringByAppendingString:@"-wal"];
if (tildefy) {
NSString* tildefiedPath = [originalPath tildefiedPath];
NSString* tildefiedShmPath = [originalShmPath tildefiedPath];
NSString* tildefiedWalPath = [originalWalPath tildefiedPath];
/* -moveItemAtPath::: will fail if there happens to already be a
file at the destination, so we clear those out first. */
[[NSFileManager defaultManager] removeItemAtPath:tildefiedPath
error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:tildefiedShmPath
error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:tildefiedWalPath
error:NULL];
[[NSFileManager defaultManager] moveItemAtPath:originalPath
toPath:tildefiedPath
error:NULL];
[[NSFileManager defaultManager] moveItemAtPath:originalShmPath
toPath:tildefiedShmPath
error:NULL] ;
[[NSFileManager defaultManager] moveItemAtPath:originalWalPath
toPath:tildefiedWalPath
error:NULL];
} else {
[[NSFileManager defaultManager] removeItemAtPath:originalPath
error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:originalShmPath
error:NULL];
[[NSFileManager defaultManager] removeItemAtPath:originalWalPath
error:NULL];
}
}
+ (NSPersistentStoreCoordinator*)persistentStoreCoordinatorType:(NSString*)storeType
identifier:(NSString*)identifier
momdName:(NSString*)momdName
options:(NSDictionary*)options
nukeAndPaveIfCorrupt:(BOOL)nukeAndPaveIfCorrupt
error_p:(NSError**)error_p {
NSPersistentStore* persistentStore = nil ;
NSArray* bundles = [NSArray arrayWithObject:[NSBundle mainAppBundle]] ;
NSManagedObjectModel* mergedMOM = [NSManagedObjectModel mergedModelFromBundles:bundles] ;
NSPersistentStoreCoordinator* newPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mergedMOM] ;
if ([storeType isEqualToString:NSSQLiteStoreType]) {
NSURL* url = [self sqliteStoreURLWithIdentifier:identifier] ;
// i.e file://localhost/Users/jk/Library/Application%20Support/BookMacster/BookMacster.sql
NSFileManager* fm = [NSFileManager defaultManager] ;
BOOL ok = YES ;
// An undocumented fact about addPersistentStoreWithType:configuration:URL:options:error:
// is that if the parent folder does not exist, the method will fail to create a
// persistent store with no explanation. So we make sure it exists
NSString* parentPath = [[url path] stringByDeletingLastPathComponent] ;
if (!parentPath) {
ok = NO ;
}
BOOL isDirectory = NO ;
BOOL fileExists = NO ;
if (ok) {
[fm fileExistsAtPath:parentPath isDirectory:&isDirectory] ;
if (fileExists && !isDirectory) {
// Someone put a file where our directory should be
ok = [fm removeItemAtPath:parentPath
error:error_p] ;
}
}
NSError* error = nil ;
if (ok && ((fileExists && !isDirectory) || !fileExists)) {
// Create parent directory
ok = [fm createDirectoryAtPath:parentPath
withIntermediateDirectories:YES
attributes:nil
error:&error] ;
}
if (!ok) {
NSString* msg = [NSString stringWithFormat:
@"Could not create directory at %@",
parentPath] ;
error = [SSYMakeError(95745, msg) errorByAddingUnderlyingError:error] ;
NSLog(@"%@", error) ;
if (error_p) {
*error_p = error ;
}
}
if (ok) {
ok = [self migrateIfNeededWithUrl:url
momdName:momdName
error:&error];
if (ok) {
// Add persistent store to it
persistentStore = [newPSC addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:url
options:options
error:error_p] ;
#if SIMULATE_BAD_STORE
if (!didSimulateBadStoreOnce) {
persistentStore = nil ;
NSString* simulatedErrorDescription = [NSString stringWithFormat:
@"Can't use this stinkin' store at\n%@",
url.path];
*error_p = SSYMakeError(12345, simulatedErrorDescription) ;
NSLog(@"61745: Simulating bad store at %@", url.path);
didSimulateBadStoreOnce = YES;
}
#endif
if (!persistentStore) {
BOOL fileExists = [fm fileExistsAtPath:[url path]] ;
if (fileExists) {
if (nukeAndPaveIfCorrupt) {
// If we did not get a store but file exists, must be a corrupt file or store.
[self removeStoreAtUrl:url
tildefy:NO];
persistentStore = [newPSC addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:url
options:options
error:error_p] ;
}
if (!persistentStore) {
if (error_p) {
NSString* msg = [NSString stringWithFormat:@"Click 'Move' to move the unreadable database\n%@\nto your desktop and start a new database. The item properties in your old database will not be available to %@.",
[url path],
[[NSBundle mainAppBundle] objectForInfoDictionaryKey:@"CFBundleExecutable"]] ;
// We used CFBundleExecutable instead of CFBundleName to get an unlocalized app name.
*error_p = [*error_p errorByAddingLocalizedRecoverySuggestion:msg] ;
NSArray* recoveryOptions = [NSArray arrayWithObjects:
[NSString localize:@"move"],
[NSString localize:@"cancel"],
nil] ;
*error_p = [*error_p errorByAddingLocalizedRecoveryOptions:recoveryOptions] ;
*error_p = [*error_p errorByAddingRecoveryAttempter:[self sharedMOCManager]] ;
*error_p = [*error_p errorByAddingUserInfoObject:url
forKey:constKeyStoreUrl] ;
}
}
}
else {
NSString* msg = [NSString stringWithFormat:
@"Could not create persistent store file at path %@",
[url absoluteString]] ;
NSLog(@"%@", msg) ;
if (error_p) {
*error_p = SSYMakeError(51298, msg) ;
}
}
}
}
}
}
else if ([storeType isEqualToString:NSInMemoryStoreType]) {
persistentStore = [newPSC addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:options
error:error_p] ;
if (!persistentStore) {
NSLog(@"Internal Error 535-1498. Failed to create inMemory persistent store") ;
}
}
if (!persistentStore) {
// If persistentStore could not be added, we don't want the
// newPSC to be returned because it won't work
[newPSC release] ;
newPSC = nil ;
}
return [newPSC autorelease] ;
}
- (NSManagedObjectContext*)managedObjectContextType:(NSString*)type
owner:(id)owner_
identifier:(NSString*)identifier
momdName:(NSString*)momdName
options:(NSDictionary*)options
nukeAndPaveIfCorrupt:(BOOL)nukeAndPaveIfCorrupt
error_p:(NSError**)error_p {
NSManagedObjectContext* managedObjectContext = nil ;
NSMutableDictionary* mocDics = nil ;
if ([type isEqualToString:NSInMemoryStoreType]) {
mocDics = [self inMemoryMOCDics] ;
}
else if ([type isEqualToString:NSSQLiteStoreType]) {
mocDics = [self sqliteMOCDics] ;
}
if (!identifier) {
identifier = @"CommonData" ;
}
NSDictionary* mocDic = [mocDics objectForKey:identifier] ;
managedObjectContext = [mocDic objectForKey:constKeyMOC] ;
if (!managedObjectContext) {
NSPersistentStoreCoordinator* coordinator = [[self class] persistentStoreCoordinatorType:type
identifier:identifier
momdName:momdName
options:options
nukeAndPaveIfCorrupt:nukeAndPaveIfCorrupt
error_p:error_p] ;
if (coordinator) {
managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType] ;
// NSLog(@"Created for %@ moc %p on thread %p isMain=%hhd", identifier, managedObjectContext, [NSThread currentThread], [[NSThread currentThread] isMainThread]) ;
[managedObjectContext setPersistentStoreCoordinator:coordinator] ;
if (!owner_) {
// A no-owner moc is a "cheap" moc.
[managedObjectContext setUndoManager:nil] ;
// Give it the default owner
owner_ = NSApp ;
}
NSDictionary* mocDic = [NSDictionary dictionaryWithObjectsAndKeys:
managedObjectContext, constKeyMOC,
owner_, constKeyOwner,
nil] ;
[mocDics setObject:mocDic
forKey:identifier] ;
[managedObjectContext release] ; // balances +alloc, above
;
}
}
return managedObjectContext ;
}
+ (void)registerOwnerDocument:(BSManagedDocument*)document
ofManagedObjectContext:(NSManagedObjectContext*)managedObjectContext {
@synchronized(self) {
NSMutableDictionary* mocDics = [[self sharedMOCManager] docMOCDics] ;
// Check for an existing entry for this owner. To make sure we
// replace it, we'll use the same key.
NSNumber* key = nil ;
for (NSNumber* aKey in mocDics) {
NSDictionary* mocDic = [mocDics objectForKey:aKey] ;
if ([mocDic objectForKey:constKeyOwner] == document) {
key = aKey ;
break ;
}
}
if (key == nil) {
// We need an arbitrary but unique key
NSInteger highestUsed = 0 ;
for (NSNumber* number in mocDics) {
highestUsed = MAX(highestUsed, [number integerValue]) ;
}
key = [NSNumber numberWithInteger:(highestUsed+1)] ;
}
NSDictionary* mocDic = [NSDictionary dictionaryWithObjectsAndKeys:
managedObjectContext, constKeyMOC,
document, constKeyOwner,
nil] ;
[mocDics setObject:mocDic
forKey:key] ;
}
}
- (id)ownerOfManagedObjectContext:(NSManagedObjectContext*)managedObjectContext
inDictionary:(NSDictionary*)dictionary {
for (NSString* identifier in dictionary) {
NSDictionary* mocDic = [dictionary objectForKey:identifier] ;
if ([mocDic objectForKey:constKeyMOC] == managedObjectContext) {
return [mocDic objectForKey:constKeyOwner] ;
}
}
return nil ;
}
- (id)ownerOfManagedObjectContext:(NSManagedObjectContext*)managedObjectContext {
id answer = nil;
@synchronized(self) {
answer = [self ownerOfManagedObjectContext:managedObjectContext
inDictionary:[self inMemoryMOCDics]] ;
if (!answer) {
answer = [self ownerOfManagedObjectContext:managedObjectContext
inDictionary:[self sqliteMOCDics]] ;
}
if (!answer) {
answer = [self ownerOfManagedObjectContext:managedObjectContext
inDictionary:[self docMOCDics]] ;
}
[answer retain];
}
[answer autorelease];
return answer ;
}
- (void)destroyManagedObjectContextWithIdentifier:(NSString*)identifier {
@synchronized(self) {
if (identifier) {
[[self inMemoryMOCDics] removeObjectForKey:identifier] ;
[[self sqliteMOCDics] removeObjectForKey:identifier] ;
[[self docMOCDics] removeObjectForKey:identifier] ;
}
}
}
- (BOOL)destroyManagedObjectContext:(NSManagedObjectContext*)managedObjectContext
inDictionary:(NSMutableDictionary*)dictionary {
NSString* removeeIdentifier = nil ;
for (NSString* identifier in dictionary) {
NSDictionary* mocDic = [dictionary objectForKey:identifier] ;
if ([mocDic objectForKey:constKeyMOC] == managedObjectContext) {
removeeIdentifier = identifier ;
break ;
}
}
if (removeeIdentifier) {
[dictionary removeObjectForKey:removeeIdentifier] ;
}
return (removeeIdentifier != nil) ;
}
- (BOOL)destroyManagedObjectContext:(NSManagedObjectContext*)managedObjectContext {
BOOL didDo = NO ;
@synchronized(self) {
didDo = [self destroyManagedObjectContext:managedObjectContext
inDictionary:[self inMemoryMOCDics]] ;
if (!didDo) {
didDo = [self destroyManagedObjectContext:managedObjectContext
inDictionary:[self sqliteMOCDics]] ;
}
if (!didDo) {
didDo = [self destroyManagedObjectContext:managedObjectContext
inDictionary:[self docMOCDics]] ;
}
}
return didDo ;
}
- (void) dealloc {
[inMemoryMOCDics release] ;
[sqliteMOCDics release] ;
[docMOCDics release] ;
[super dealloc] ;
}
+ (NSManagedObjectContext*)managedObjectContextType:(NSString*)type
owner:(id)owner
identifier:(NSString*)identifier
momdName:(NSString*)momdName
nukeAndPaveIfCorrupt:(BOOL)nukeAndPaveIfCorrupt
error_p:(NSError**)error_p {
NSDictionary* options = nil ;
NSManagedObjectContext* moc = [[self sharedMOCManager] managedObjectContextType:type
owner:owner
identifier:identifier
momdName:momdName
options:options
nukeAndPaveIfCorrupt:nukeAndPaveIfCorrupt
error_p:error_p] ;
return moc ;
}
+ (id)ownerOfManagedObjectContext:(NSManagedObjectContext*)managedObjectContext {
return [[self sharedMOCManager] ownerOfManagedObjectContext:managedObjectContext] ;
}
+ (BOOL)destroyManagedObjectContext:(NSManagedObjectContext*)managedObjectContext {
return [[self sharedMOCManager] destroyManagedObjectContext:managedObjectContext] ;
}
+ (void)destroyManagedObjectContextWithIdentifier:(NSString*)identifier {
[[self sharedMOCManager] destroyManagedObjectContextWithIdentifier:identifier] ;
}
+ (void)removeSqliteStoreForIdentifier:(NSString*)identifier {
NSURL* url = [self sqliteStoreURLWithIdentifier:identifier];
[self removeStoreAtUrl:url
tildefy:NO];
}
+ (BOOL)moc:(NSManagedObjectContext*)moc
isInDic:(NSDictionary*)dic {
// The outer key is the serial number assigned in -registerOwnerDocument::
// The pointer to the moc is a value for the inner key constKeyMoc
for (NSNumber* number in dic) {
if ([[dic objectForKey:number] objectForKey:constKeyMOC] == moc) {
return YES ;
}
}
return NO ;
}
+ (BOOL)isInMemoryMOC:(NSManagedObjectContext*)moc {
return [self moc:moc
isInDic:[[self sharedMOCManager] inMemoryMOCDics]] ;
}
+ (BOOL)isSqliteMOC:(NSManagedObjectContext*)moc {
return [self moc:moc
isInDic:[[self sharedMOCManager] sqliteMOCDics]] ;
}
+ (BOOL)isDocMOC:(NSManagedObjectContext*)moc {
BOOL answer = NO;
@synchronized(self) {
answer = [self moc:moc
isInDic:[[self sharedMOCManager] docMOCDics]] ;
}
return answer;
}
+ (NSManagedObjectContext*)scratchManagedObjectContext {
NSManagedObjectContextConcurrencyType concurrencyType = [[NSThread currentThread] isMainThread] ? NSMainQueueConcurrencyType : NSPrivateQueueConcurrencyType;
NSManagedObjectContext* scratchMOC = [[NSManagedObjectContext alloc] initWithConcurrencyType:concurrencyType];
scratchMOC.undoManager = nil;
NSArray* bundles = [NSArray arrayWithObject:[NSBundle mainAppBundle]] ;
NSManagedObjectModel* mergedMOM = [NSManagedObjectModel mergedModelFromBundles:bundles] ;
NSPersistentStoreCoordinator* scratchPSC = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:mergedMOM] ;
NSError* unlikelyError = nil;
[scratchPSC addPersistentStoreWithType:NSInMemoryStoreType
configuration:nil
URL:nil
options:0
error:&unlikelyError];
NSAssert(unlikelyError == nil, @"Internal Error 874-4408 %@", unlikelyError);
scratchMOC.persistentStoreCoordinator = scratchPSC;
[scratchPSC release];
[scratchMOC autorelease];
return scratchMOC;
}
+ (NSManagedObjectContext*)pasteboardManagedObjectContext {
@synchronized(self) {
if (!static_pasteboardManagedObjectContext) {
static_pasteboardManagedObjectContext = [self scratchManagedObjectContext] ;
#if !__has_feature(objc_arc)
[static_pasteboardManagedObjectContext retain];
#endif
}
}
// No autorelease. This sticks around forever.
return static_pasteboardManagedObjectContext ;
}
+ (NSArray <NSManagedObject*>*)deepCopiesForPasteboardObjects:(NSArray <NSManagedObject*> *)objects
doNotEnterRelationshipNames:(NSSet*)relationshipNames {
NSMutableArray* copies = [NSMutableArray new];
for (NSManagedObject* object in objects) {
NSManagedObject* copy = [object deepCopyInManagedObjectContext:[self pasteboardManagedObjectContext]
doNotEnterRelationshipNames:relationshipNames];
[copies addObject:copy];
}
NSArray <NSManagedObject*> * answer = [copies copy];
#if !__has_feature(objc_arc)
[copies release];
[answer autorelease];
#endif
return answer;
}
#if DEBUG
+ (void)logDebugCurrentSqliteMocs {
NSDictionary* sqliteMOCDics = [[self sharedMOCManager] sqliteMOCDics] ;
NSLog(@"Listing of SSYManaged SQLite MOCs") ;
for (NSString* identifier in sqliteMOCDics) {
NSManagedObjectContext* moc = [[sqliteMOCDics objectForKey:identifier] objectForKey:constKeyMOC] ;
id owner = [[sqliteMOCDics objectForKey:identifier] objectForKey:constKeyOwner] ;
NSCountedSet* stats = [[NSCountedSet alloc] init] ;
for (NSManagedObject* object in [moc registeredObjects]) {
NSString* entityName = [[object entity] name] ;
[stats addObject:entityName] ;
}
NSLog(@" moc %p owned by %@ at %@\n Object Counts: %@",
moc,
[owner shortDescription],
[[[[moc store1] URL] path] lastPathComponent],
[stats shortDescription]) ;
[stats release] ;
}
}
#endif
@end
// Note 1.
// Because our method +persistentStoreCoordinatorType:::::: always creates
// a new persistent store coordinator and always adds exactly one persistent
// store to it, we can just grab its first (and only) store.