-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNSManagedObjectContext+Additions.m
More file actions
190 lines (145 loc) · 6.57 KB
/
Copy pathNSManagedObjectContext+Additions.m
File metadata and controls
190 lines (145 loc) · 6.57 KB
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
//
// NSManagedObjectContext+Additions.m
//
// Created by Wess Cope on 9/23/11.
// Copyright 2012. All rights reserved.
//
#import "NSManagedObjectContext+Additions.h"
@implementation NSManagedObjectContext(Additions)
- (NSManagedObjectModel *)objectModel
{
return [[self persistentStoreCoordinator] managedObjectModel];
}
#pragma mark - Sync methods
- (NSArray *)fetchObjectsForEntity:(NSString *)entity
{
return [self fetchObjectsForEntity:entity predicate:nil sortDescriptors:nil];
}
- (NSArray *)fetchObjectsForEntity:(NSString *)entity predicate:(NSPredicate *)predicate
{
return [self fetchObjectsForEntity:entity predicate:predicate sortDescriptors:nil];
}
- (NSArray *)fetchObjectsForEntity:(NSString *)entity sortDescriptors:(NSArray *)sortDescriptors
{
return [self fetchObjectsForEntity:entity predicate:nil sortDescriptors:sortDescriptors];
}
- (NSArray *)fetchObjectsForEntity:(NSString *)entity predicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors
{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntity:[NSEntityDescription entityForName:entity inManagedObjectContext:self] predicate:predicate sortDescriptors:sortDescriptors];
NSError *error = nil;
@try
{
NSArray *results = [self executeFetchRequest:request error:&error];
if(error)
{
@throw [NSString stringWithFormat:@"CoreData Fetch error: %@", [error userInfo]];
return nil;
}
return results;
}
@catch (NSException *exception)
{
NSLog(@"Fetch Exception: %@", [exception description]);
}
return nil;
}
- (id)fetchObjectForEntity:(NSString *)entity
{
return [self fetchObjectForEntity:entity predicate:nil sortDescriptors:nil];
}
- (id)fetchObjectForEntity:(NSString *)entity predicate:(NSPredicate *)predicate
{
return [self fetchObjectForEntity:entity predicate:predicate sortDescriptors:nil];
}
- (id)fetchObjectForEntity:(NSString *)entity sortDescriptors:(NSArray *)sortDescriptors
{
return [self fetchObjectForEntity:entity predicate:nil sortDescriptors:sortDescriptors];
}
- (id)fetchObjectForEntity:(NSString *)entity predicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors
{
NSArray *results = [self fetchObjectsForEntity:entity predicate:predicate sortDescriptors:sortDescriptors];
if (results.count < 1)
return nil;
NSLog(@"results; %@", results);
return nil; //[results objectAtIndex:0];
}
#pragma mark - Async Methods
- (void)fetchObjectsForEntity:(NSString *)entity callback:(FetchObjectsCallback)callback
{
[self fetchObjectsForEntity:entity predicate:nil sortDescriptors:nil callback:callback];
}
- (void)fetchObjectsForEntity:(NSString *)entity predicate:(NSPredicate *)predicate callback:(FetchObjectsCallback)callback
{
[self fetchObjectsForEntity:entity predicate:predicate sortDescriptors:nil callback:callback];
}
- (void)fetchObjectsForEntity:(NSString *)entity sortDescriptors:(NSArray *)sortDescriptors callback:(FetchObjectsCallback)callback
{
[self fetchObjectsForEntity:entity predicate:nil sortDescriptors:sortDescriptors callback:callback];
}
- (void)fetchObjectsForEntity:(NSString *)entity predicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors callback:(FetchObjectsCallback)callback
{
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entity inManagedObjectContext:self];
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntity:entityDescription predicate:predicate sortDescriptors:sortDescriptors];
[self fetchRequest:request withCallback:callback];
}
- (void)fetchObjectForEntity:(NSString *)entity callback:(FetchObjectCallback)callback
{
[self fetchObjectForEntity:entity predicate:nil sortDescriptors:nil callback:callback];
}
- (void)fetchObjectForEntity:(NSString *)entity predicate:(NSPredicate *)predicate callback:(FetchObjectCallback)callback
{
[self fetchObjectForEntity:entity predicate:predicate sortDescriptors:nil callback:callback];
}
- (void)fetchObjectForEntity:(NSString *)entity sortDescriptors:(NSArray *)sortDescriptors callback:(FetchObjectCallback)callback
{
[self fetchObjectForEntity:entity predicate:nil sortDescriptors:sortDescriptors callback:callback];
}
- (void)fetchObjectForEntity:(NSString *)entity predicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors callback:(FetchObjectCallback)callback
{
[self fetchObjectsForEntity:entity predicate:predicate sortDescriptors:sortDescriptors callback:^(NSArray *objects, NSError *error) {
id object = nil;
if(objects.count > 0)
object = [objects objectAtIndex:0];
callback(object, error);
}];
}
- (void)fetchRequest:(NSFetchRequest *)fetchRequest withCallback:(FetchObjectsCallback)callback
{
NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:[self persistentStoreCoordinator]];
dispatch_async(GLOBAL_QUEUE, ^{
NSError *error = nil;
NSArray *objects = [context executeFetchRequest:fetchRequest error:&error];
NSMutableArray *objectIds = [NSMutableArray arrayWithCapacity:objects.count];
[objects each:^(id item) {
[objectIds addObject:[(NSManagedObject *)item objectID]];
}];
dispatch_async(MAIN_QUEUE, ^{
NSMutableArray *resultObjects = [NSMutableArray arrayWithCapacity:objectIds.count];
[objectIds each:^(id item) {
[resultObjects addObject:[self objectWithID:(NSManagedObjectID *)item]];
}];
callback([NSArray arrayWithArray:resultObjects], error);
});
});
}
#pragma mark - Insert New Entity
- (id)insertEntity:(NSString *)entity
{
return [NSEntityDescription insertNewObjectForEntityForName:entity inManagedObjectContext:self];
}
- (void)deleteEntity:(NSString *)entity withPredicate:(NSPredicate *)predicate
{
NSError __block *error = nil;
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntity:[NSEntityDescription entityForName:entity inManagedObjectContext:self] predicate:predicate];
NSArray *results = [self executeFetchRequest:fetchRequest error:&error];
[results each:^(id item) {
NSManagedObject *object = (NSManagedObject *)item;
if([object validateForDelete:&error])
NSLog(@"CoreData Delete error: %@", [error userInfo]);
else
[self deleteObject:object];
}];
[self save:&error];
}
@end