-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYMojo.m
335 lines (289 loc) · 10.8 KB
/
SSYMojo.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
#import "SSYMojo.h"
#import "NSError+InfoAccess.h"
#import "NSError+MyDomain.h"
#import "SSYAlert.h"
#import "NSManagedObjectContext+Cheats.h"
#import "NSObject+MoreDescriptions.h"
#import "NSEntityDescription+SSYMavericksBugFix.h"
// For debugging
@implementation SSYMojo
@synthesize managedObjectContext = m_managedObjectContext ;
@synthesize entityName ;
- (id)initWithManagedObjectContext:(NSManagedObjectContext*)managedObjectContext
entityName:(NSString*)entityName_ {
if (managedObjectContext && entityName_) {
self = [super init] ;
if (self != nil) {
[self setManagedObjectContext:managedObjectContext] ;
[self setEntityName:entityName_] ;
}
}
else {
// See http://lists.apple.com/archives/Objc-language/2008/Sep/msg00133.html ...
[super dealloc] ;
self = nil ;
}
return self ;
}
- (NSString*)description {
return [NSString stringWithFormat:
@"<%@ %p> entity=%@ store=%@",
[self className],
self,
[self entityName],
[[[self managedObjectContext] store1] longDescription]] ;
}
- (void)dealloc {
[m_managedObjectContext release] ;
[entityName release] ;
[super dealloc];
}
- (NSArray*)objectsWithSubpredicates:(NSArray*)subpredicates
type:(NSCompoundPredicateType)type
error_p:(NSError**)error_p {
NSPredicate* compoundPredicate = nil ;
if (subpredicates) {
switch (type) {
case NSAndPredicateType:
compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates] ;
break ;
case NSOrPredicateType:
compoundPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates] ;
break ;
default:
NSLog(@"Internal Error 842-0828. NSCompoundPredicateType %ld not supported", (long)type) ;
}
}
NSError* error = nil ;
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init] ;
NSManagedObjectContext* managedObjectContext = [self managedObjectContext] ;
NSArray* fetches = nil ;
if (managedObjectContext) {
[fetchRequest setEntity:[NSEntityDescription SSY_entityForName:[self entityName]
inManagedObjectContext:managedObjectContext]] ;
if (compoundPredicate) {
[fetchRequest setPredicate:compoundPredicate] ;
}
@try {
fetches = [managedObjectContext executeFetchRequest:fetchRequest
error:&error] ;
}
@catch (NSException *exception) {
if (!error) {
error = SSYMakeError(149045, @"Exception fetching with subpredicates") ;
}
error = [error errorByAddingUnderlyingException:exception] ;
}
if (error) {
error = [error errorByAddingUserInfoObject:compoundPredicate
forKey:@"Predicate"] ;
error = [error errorByAddingUserInfoObject:entityName
forKey:@"Entity Name"] ;
NSLog(@"Internal Error 487-2762: %@", [error longDescription]) ;
if (error_p) {
*error_p = error ;
}
}
}
[fetchRequest release] ;
return fetches ;
}
- (NSArray*)objectsWithPredicate:(NSPredicate*)predicate
error_p:(NSError**)error_p {
NSArray* subpredicates ;
if (predicate) {
subpredicates = [NSArray arrayWithObject:predicate] ;
}
else {
subpredicates = nil ;
}
NSArray* objects = [self objectsWithSubpredicates:subpredicates
type:NSAndPredicateType
error_p:error_p] ;
return objects ;
}
- (NSManagedObject*)objectWithPredicate:(NSPredicate*)predicate
error_p:(NSError**)error_p {
NSArray* fetches = [self objectsWithPredicate:predicate
error_p:error_p] ;
NSManagedObject* finding = nil ;
if ([fetches count] > 0) {
finding = [fetches objectAtIndex:0] ;
}
return finding ;
}
- (NSArray*)objectsWithDirectPredicateLeftExpression:(NSExpression*)lhs
rightExpression:(NSExpression*)rhs
operatorType:(NSPredicateOperatorType)operatorType
error_p:(NSError**)error_p {
NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:operatorType
options:0] ;
return [self objectsWithPredicate:predicate
error_p:error_p] ;
}
- (NSManagedObject*)objectWithDirectPredicateLeftExpression:(NSExpression*)lhs
rightExpression:(NSExpression*)rhs
operatorType:(NSPredicateOperatorType)operatorType
error_p:(NSError**)error_p {
NSPredicate *predicate = [NSComparisonPredicate predicateWithLeftExpression:lhs
rightExpression:rhs
modifier:NSDirectPredicateModifier
type:operatorType
options:0] ;
return [self objectWithPredicate:predicate
error_p:error_p] ;
}
+ (NSArray*)allObjectsForEntityName:(NSString*)entityName
managedObjectContext:(NSManagedObjectContext*)managedObjectContext
error_p:(NSError**)error_p {
NSError* error = nil ;
NSFetchRequest* fetchRequest ;
fetchRequest = [[NSFetchRequest alloc] init] ;
// The following was an interesting experiment. In BookMacster 1.11, it caused
// a "serious Core Data error" and also an error what looked like trying to bind
// the Sync Log popup menu to a deallocated object in -[SSYArrayController selectFirstArrangedObject],
// when it invoked -[NSArrayController removeSelectionIndexes:[self selectionIndexes]] ;
// [managedObjectContext processPendingChanges] ; // Do not do this
NSEntityDescription* entity = [NSEntityDescription SSY_entityForName:entityName
inManagedObjectContext:managedObjectContext] ;
NSArray* allObjects ;
if (entity) {
[fetchRequest setEntity:entity] ;
// Since we didn't set a predicate in fetchRequest, we get all objects
@try {
allObjects = [managedObjectContext executeFetchRequest:fetchRequest
error:&error] ;
}
@catch (NSException *exception) {
if (!error) {
error = SSYMakeError(149046, @"Exception fetching all objects") ;
}
error = [error errorByAddingUnderlyingException:exception] ;
}
if (error) {
error = [error errorByAddingUserInfoObject:entityName
forKey:@"Entity Name"] ;
NSLog(@"Internal Error 915-1874 %@", [error localizedDescription]) ;
allObjects = nil ;
}
}
else {
// Defensive programming added in BookMacster 1.19.2. If data model
// is not available, entity will be nil and if we didn't test for that,
// -[NSManagedObjectContext executeFetchRequest:error] would crash
// due to nil entity in fetch request.
NSString* errorDesc = [NSString stringWithFormat:@"No entity for %@", entityName] ;
NSLog(@"Internal Error 915-0893 %@", errorDesc) ;
error = SSYMakeError(262049, errorDesc) ;
error = [error errorByAddingInfoToExplainMissingAppResource] ;
allObjects = nil ;
}
[fetchRequest release] ;
if (error && error_p) {
*error_p = error ;
}
return allObjects ;
}
- (NSArray*)allObjectsError_p:(NSError**)error_p {
return [[self class] allObjectsForEntityName:[self entityName]
managedObjectContext:[self managedObjectContext]
error_p:error_p] ;
}
- (NSArray*)allObjects {
NSError* error = nil ;
NSArray* allObjects = [self allObjectsError_p:&error] ;
if (error) {
[SSYAlert alertError:error] ;
}
return allObjects ;
}
- (NSManagedObject*)freshObject {
NSManagedObjectContext* moc = [self managedObjectContext] ;
NSManagedObject* object = [NSEntityDescription insertNewObjectForEntityForName:[self entityName]
inManagedObjectContext:moc] ;
#if 0
// Added in BookMacster 1.16 to try and find a rare bug
// Removed in BookMacster 1.17 because we now have a better method,
// in -[Starker freshStark]
if (![[NSThread currentThread] isMainThread]) {
NSLog(@"Internal Error 523-0024 %@\n%@",
[self entityName],
SSYDebugBacktraceDepth(8)) ;
}
#endif
return object ;
}
- (NSManagedObject*)objectWithID:(NSManagedObjectID*)objectID {
return [[self managedObjectContext] objectWithID:objectID] ;
}
- (void)insertObject:(NSManagedObject*)object {
[[self managedObjectContext] insertObject:object] ;
}
- (void)deleteObject:(NSManagedObject*)object {
[[self managedObjectContext] deleteObject:object] ;
}
- (BOOL)deleteAllObjectsError_p:(NSError**)error_p {
NSError* error = nil ;
NSArray* allObjects = [self allObjectsError_p:&error] ;
if (error) {
if (error_p) {
*error_p = error ;
}
goto end ;
}
NSManagedObjectContext* moc = [self managedObjectContext] ;
for (NSManagedObject* object in allObjects) {
[moc deleteObject:object] ;
}
end:
return (error == nil) ;
}
- (BOOL)save:(NSError**)error_p {
NSError* error = nil ;
BOOL ok = NO ;
@try {
ok = [[self managedObjectContext] save:&error] ;
// The following was added in BookMacster 1.12 because Cocoa error
// 134030 error is sometimes received from users and, and I think it's
// probably coming from here, but I want to prove it. With this change,
// 134030 will now be the underlying error.
if (!ok) {
error = [SSYMakeError(149034, @"Error saving local settings") errorByAddingUnderlyingError:error] ;
}
}
@catch (NSException* exception) {
ok = NO ;
if (!error) {
error = SSYMakeError(149035, @"Exception saving local settings") ;
}
error = [error errorByAddingUnderlyingException:exception] ;
}
if (error_p) {
NSPersistentStore* store = [[self managedObjectContext] store1] ;
error = [error errorByAddingUserInfoObject:[store description]
forKey:@"Store"] ;
error = [error errorByAddingUserInfoObject:[[store URL] path]
forKey:@"Path"] ;
*error_p = error ;
}
return ok ;
}
- (NSURL*)deleteFileError_p:(NSError**)error_p {
NSManagedObjectContext* managedObjectContext = [self managedObjectContext] ;
NSURL* storeUrl = [[managedObjectContext store1] URL] ;
NSString* path = [storeUrl path] ;
NSError* error = nil ;
BOOL ok = [[NSFileManager defaultManager] removeItemAtPath:path
error:&error] ;
if (!ok) {
storeUrl = nil ;
if (error_p) {
*error_p = error ;
}
}
return storeUrl ;
}
@end