forked from brightsoftdev/Additions-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNSManagedObject+Additions.m
More file actions
140 lines (111 loc) · 3.21 KB
/
NSManagedObject+Additions.m
File metadata and controls
140 lines (111 loc) · 3.21 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
//
// NSManagedObject+Additions.m
//
// Created by Wess Cope on 9/23/11.
// Copyright 2012. All rights reserved.
//
#import "NSManagedObject+Additions.h"
static NSManagedObjectContext *context = nil;
@implementation NSManagedObject(Additions)
//#pragma mark - Class methods
//+ (void)setContext:(NSManagedObjectContext *)context;
//+ (NSManagedObjectContext *)context;
//+ (NSString *)name;
//+ (NSEntityDescription *)description;
//+ (id)new;
//+ (id)new:(NSDictionary *)details;
//+ (id)count;
//+ (id)all;
//+ (id)filterWithPredicate:(NSPredicate *)predicate;
//+ (id)filterWithSortDescriptors:(NSArray *)sortDescriptors;
//+ (id)filterWithPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors;
//+ (id)getWithPredicate:(NSPredicate *)predicate;
//+ (id)deleteAll;
//
//#pragma mark - Instance methods
//- (BOOL)save;
#pragma mark - Class Methods
+ (void)setContext:(NSManagedObjectContext *)aContext
{
context = aContext;
}
+ (NSManagedObjectContext *)context
{
//#warning RETURN SINGLETON INSTANCE OF managedObjectContext;
if (context == nil)
return nil; //[WCCoreData instance].managedObjectContext;
return context;
}
- (NSManagedObjectContext *)context
{
return [[self class] context];
}
+ (NSString *)name
{
return [NSString stringWithCString:object_getClassName(self) encoding:NSASCIIStringEncoding];
}
+ (NSEntityDescription *)description
{
return [NSEntityDescription entityForName:[self name] inManagedObjectContext:[self context]];
}
+ (id)new
{
return [NSEntityDescription insertNewObjectForEntityForName:[self name] inManagedObjectContext:[self context]];
}
+ (id)new:(NSDictionary *)details
{
id instance = [self new];
[details each:^(id key, id obj) {
[instance setValue:obj forKey:key];
}];
return instance;
}
+ (NSNumber *)count
{
return [self countwithPredicate:nil];
}
+ (NSNumber *)countwithPredicate:(NSPredicate *)predicate
{
NSError *error = nil;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntity:[self description] predicate:predicate];
int cnt = [[self context] countForFetchRequest:request error:&error];
return [NSNumber numberWithInt:cnt];
}
+ (NSArray *)all
{
return [[self context] fetchObjectsForEntity:[self name]];
}
+ (id)filterWithPredicate:(NSPredicate *)predicate
{
return [self filterWithPredicate:predicate sortDescriptors:nil];
}
+ (id)filterWithSortDescriptors:(NSArray *)sortDescriptors
{
return [self filterWithPredicate:nil sortDescriptors:sortDescriptors];
}
+ (id)filterWithPredicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)sortDescriptors
{
return [[self context] fetchObjectsForEntity:[self name] predicate:predicate sortDescriptors:sortDescriptors];
}
+ (id)getWithPredicate:(NSPredicate *)predicate
{
return [[self context] fetchObjectForEntity:[self name] predicate:predicate];
}
+ (void)deleteAll
{
[[self all] each:^(id item){
[[self context] deleteObject:(NSManagedObject *)item];
}];
}
#pragma mark - Instance methods
- (BOOL)save
{
NSError *error = nil;
if(![[self context] save:&error])
{
NSLog(@"CoreData save error: %@", [error userInfo]);
return NO;
}
return YES;
}
@end