-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSManagedObject+Data.m
More file actions
189 lines (152 loc) · 4.73 KB
/
Copy pathNSManagedObject+Data.m
File metadata and controls
189 lines (152 loc) · 4.73 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
//
// NSManagedObject+Data.m
//
// Created by Maurice Raguse on 30.06.15.
// Copyright (c) 2015 Maurice Raguse All rights reserved.
//
// Version 1.0
#import "NSManagedObject+Data.h"
#import "AppDelegate.h"
@implementation NSManagedObject (Data)
+(void)save
{
AppDelegate * appDelegate = [[UIApplication sharedApplication]delegate];
[appDelegate saveContext];
}
+(NSManagedObjectContext*)moc
{
AppDelegate * appDelegate = [[UIApplication sharedApplication]delegate];
return appDelegate.managedObjectContext;
}
+(void)deleteAllEntities
{
NSString *name = NSStringFromClass(self.class);
[self deleteEntitiesForName:name withPredicate:nil];
}
+(void)deleteAllEntitiesForName:(NSString*)name
{
[self deleteEntitiesForName:name withPredicate:nil];
}
+(void)deleteEntitiesWithPredicate:(NSPredicate*)predicate
{
NSString *name = NSStringFromClass(self.class);
[self deleteEntitiesForName:name withPredicate:predicate];
}
/**
* Deletes the object without force saving the context!
* @author Maurice Raguse
*/
-(void)deleteEntity
{
[self.managedObjectContext deleteObject:self];
}
-(void)deleteEntityAndSaveContext:(BOOL)shouldSaveContext
{
[self.managedObjectContext deleteObject:self];
if (shouldSaveContext)
{
[NSManagedObject save];
}
}
+(void)deleteEntitiesForName:(NSString*)name withPredicate:(NSPredicate*)predicate
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:name];
if (predicate) {
[fetchRequest setPredicate:predicate];
}
NSError* error;
NSArray* allEntities = [[self moc] executeFetchRequest:fetchRequest error:&error];
if (error)
{
NSLog(@"Error: %@", error.localizedDescription);
abort();
}
for (NSManagedObject *Entity in allEntities)
{
[Entity.managedObjectContext deleteObject:Entity];
}
[self save];
}
+(NSArray*)allEntitiesWithSortDescriptors:(NSArray*)sortDescriptors
{
NSString *name = NSStringFromClass(self.class);
return [self allEntitiesForName:name sortDescriptors:sortDescriptors];
}
+(NSArray*)allEntitiesForName:(NSString*)name sortDescriptors:(NSArray*)sortDescriptors
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:name];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError* error;
NSArray* allEntities = [[self moc] executeFetchRequest:fetchRequest error:&error];
if (error)
{
NSLog(@"Error: %@", error.localizedDescription);
abort();
}
return allEntities;
}
+(NSArray*)entitiesWithPredicate:(NSPredicate*)predicate sortDescriptors:(NSArray*)sortDescriptors
{
NSString *name = NSStringFromClass(self.class);
return [self entitiesForName:name withPredicate:predicate sortDescriptors:sortDescriptors];
}
+(NSArray*)entitiesForName:(NSString*)name withPredicate:(NSPredicate*)predicate sortDescriptors:(NSArray*)sortDescriptors
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:name];
[fetchRequest setPredicate:predicate];
fetchRequest.sortDescriptors = sortDescriptors;
NSError* error;
NSArray* allEntities = [[self moc] executeFetchRequest:fetchRequest error:&error];
if (error)
{
NSLog(@"Error: %@", error.localizedDescription);
abort();
}
return allEntities;
}
+(NSArray*)entitiesWithPredicate:(NSPredicate*)predicate
{
return [self entitiesWithPredicate:predicate sortDescriptors:nil];
}
+(NSArray*)entitiesForName:(NSString*)name withPredicate:(NSPredicate*)predicate
{
return [self entitiesForName:name withPredicate:predicate sortDescriptors:nil];
}
+(instancetype)entityWithPredicate:(NSPredicate*)predicate
{
NSString *name = NSStringFromClass(self.class);
return [self entityForName:name withPredicate:predicate];
}
+(instancetype)entityForName:(NSString*)name withPredicate:(NSPredicate*)predicate
{
NSArray* allEntities = [self entitiesForName:name withPredicate:predicate];
if (allEntities && allEntities.count > 0)
{
return allEntities[0];
}
return nil;
}
/**
* Creates an new instance from self classname type.
* @author Maurice Raguse
*
* @return The new entity instance.
*/
+(instancetype)createEntity
{
NSString *name = NSStringFromClass(self.class);
return [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:[self moc]];;
}
/**
* Creates an new instance for an entity type.
* @author Maurice Raguse
*
* @return The new entity instance.
*
* @param name The class name of the entity.
*/
+(instancetype)createEntityForName:(NSString*)name
{
return [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:[self moc]];;
}
@end