-
Notifications
You must be signed in to change notification settings - Fork 1
/
CoreData+Convenience.m
139 lines (95 loc) · 5.06 KB
/
CoreData+Convenience.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
//
// CoreData+Convenience.m
// Trend
//
// Created by Alexander Ivanov on 03.09.17.
// Copyright © 2017 Pine 9. All rights reserved.
//
#import "CoreData+Convenience.h"
@implementation NSPersistentContainer (Convenience)
- (void)loadPersistentStores:(void (^)(NSPersistentStoreDescription *))block {
[self loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *description, NSError *error) {
if (block)
block(description);
[error log:@"loadPersistentStoresWithCompletionHandler:"];
}];
}
+ (void)loadPersistentContainerWithName:(NSString *)name completionHandler:(void (^)(NSPersistentContainer *))block {
NSPersistentContainer *container = [NSPersistentContainer persistentContainerWithName:name];
[container loadPersistentStores:^(NSPersistentStoreDescription *description) {
if (block)
block(container);
}];
}
@end
@implementation NSManagedObject (Convenience)
+ (NSString *)entityName {
return [self description];
}
+ (instancetype)insertInContext:(NSManagedObjectContext *)context {
return [NSEntityDescription insertNewObjectForEntityForName:[self description] inManagedObjectContext:context];
}
+ (NSArray *)executeFetchRequestInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate sortDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors fetchLimit:(NSUInteger)fetchLimit {
return [context executeFetchRequestWithEntityName:[self entityName] predicate:predicate sortDescriptors:sortDescriptors fetchLimit:fetchLimit];
}
+ (NSArray *)executeFetchRequestInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate sortDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors {
return [context executeFetchRequestWithEntityName:[self entityName] predicate:predicate sortDescriptors:sortDescriptors fetchLimit:0];
}
+ (NSArray *)executeFetchRequestInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate {
return [context executeFetchRequestWithEntityName:[self entityName] predicate:predicate sortDescriptors:Nil fetchLimit:0];
}
+ (NSArray *)executeFetchRequestInContext:(NSManagedObjectContext *)context predicateWithFormat:(NSString *)format, ... {
va_list args;
va_start(args, format);
NSPredicate *predicate = [NSPredicate predicateWithFormat:format arguments:args];
va_end(args);
return [context executeFetchRequestWithEntityName:[self entityName] predicate:predicate sortDescriptors:Nil fetchLimit:0];
}
+ (__kindof NSManagedObject *)executeFetchRequestInContext:(NSManagedObjectContext *)context firstObject:(NSString *)attributeName {
return [context executeFetchRequestWithEntityName:[self entityName] predicate:Nil sortDescriptors:@[ [[NSSortDescriptor alloc] initWithKey:attributeName ascending:YES] ] fetchLimit:1].firstObject;
}
+ (__kindof NSManagedObject *)executeFetchRequestInContext:(NSManagedObjectContext *)context lastObject:(NSString *)attributeName {
return [context executeFetchRequestWithEntityName:[self entityName] predicate:Nil sortDescriptors:@[ [[NSSortDescriptor alloc] initWithKey:attributeName ascending:NO] ] fetchLimit:1].lastObject;
}
+ (NSUInteger)countForFetchRequestInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate {
return [context countForFetchRequestWithEntityName:[self entityName] predicate:predicate];
}
+ (NSUInteger)countForFetchRequestInContext:(NSManagedObjectContext *)context predicateWithFormat:(NSString *)format, ... {
va_list args;
va_start(args, format);
NSPredicate *predicate = [NSPredicate predicateWithFormat:format arguments:args];
va_end(args);
return [context countForFetchRequestWithEntityName:[self entityName] predicate:predicate];
}
@end
@implementation NSManagedObjectContext (Convenience)
- (BOOL)save {
NSError *error = Nil;
BOOL save = [self save:&error];
[error log:@"save:"];
return save;
}
- (NSManagedObject *)insertObjectForName:(NSString *)entityName {
return [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self];
}
- (NSArray *)executeFetchRequestWithEntityName:(NSString *)entityName predicate:(NSPredicate *)predicate sortDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors fetchLimit:(NSUInteger)fetchLimit {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self];
fetchRequest.predicate = predicate;
fetchRequest.sortDescriptors = sortDescriptors;
fetchRequest.fetchLimit = fetchLimit;
NSError *error = Nil;
NSArray *fetchedObjects = [self executeFetchRequest:fetchRequest error:&error];
[error log:@"executeFetchRequest:"];
return fetchedObjects;
}
- (NSUInteger)countForFetchRequestWithEntityName:(NSString *)entityName predicate:(NSPredicate *)predicate {
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self];
fetchRequest.predicate = predicate;
NSError *error = Nil;
NSUInteger count = [self countForFetchRequest:fetchRequest error:&error];
[error log:@"countForFetchRequest:"];
return count;
}
@end