-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathNSManagedObjectContextX.m
40 lines (31 loc) · 1.28 KB
/
NSManagedObjectContextX.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
#import "NSManagedObjectContextX.h"
@implementation NSManagedObjectContext (X)
- (NSManagedObject *)newObjectOfEntity:(NSString *)name
{
return [NSManagedObject.alloc initWithEntity:self.persistentStoreCoordinator.managedObjectModel.entitiesByName[name] insertIntoManagedObjectContext:self];
}
- (NSArray *)objectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit
{
NSFetchRequest *request = NSFetchRequest.new;
request.entity = self.persistentStoreCoordinator.managedObjectModel.entitiesByName[name];
request.predicate = predicate;
request.fetchLimit = limit;
if (sortDescriptors)
request.sortDescriptors = sortDescriptors;
return [self executeFetchRequest:request error:nil];
}
- (void)deleteObjects:(NSArray *)objects
{
for (NSManagedObject *object in objects)
@try
{
[self deleteObject:object];
}
@catch (NSException *exception)
{}
}
- (void)deleteObjectsOfEntity:(NSString *)name withPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors andFetchLimit:(NSUInteger)limit
{
[self deleteObjects:[self objectsOfEntity:name withPredicate:predicate sortDescriptors:sortDescriptors andFetchLimit:limit]];
}
@end