-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneral.m
60 lines (45 loc) · 2.3 KB
/
General.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
#import "General.h"
@interface General ()
// Private interface goes here.
@end
@implementation General
+(id)addOrUpdateObjectFromDictionary:(NSDictionary *)dictionary{
if([dictionary isKindOfClass:[NSDictionary class]]){
return [self addOrUpdateObjectFromDictionary:dictionary withClassName:[[[self class] description] stringByReplacingOccurrencesOfString:@"BCD" withString:@""]];
}else if([dictionary isKindOfClass:[NSArray class]]){
return [self addOrUpdateObjectFromDictionary:[(NSArray *)dictionary firstObject] withClassName:[[[self class] description] stringByReplacingOccurrencesOfString:@"BCD" withString:@""]];
}else{
return nil;
}
}
+(id)addOrUpdateObjectFromDictionary:(NSDictionary *)dictionary withClassName:(NSString *)className{
NSThread *currentThread = [NSThread currentThread];
NSManagedObjectContext *privateContext = [[currentThread threadDictionary] objectForKey:@"managedObjectContext"];
if(!privateContext){
privateContext = [[LocalManager sharedManager] managedObjectContext];
}
id obj = [[LocalManager sharedManager] getUnfilteredObjectWithClass:[className stringByReplacingOccurrencesOfString:@"BCD" withString:@""] withId:[dictionary objectForKey:@"id"]];
if(!obj){
obj = [NSEntityDescription insertNewObjectForEntityForName:[className stringByReplacingOccurrencesOfString:@"BCD" withString:@""] inManagedObjectContext:privateContext];
}
[obj safeSetValuesForKeysWithDictionary:dictionary];
return obj;
}
+(BOOL)doesObjectExistsWithClass:(NSString *)className withId:(NSString *)objectId{
NSManagedObject *obj;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSThread *currentThread = [NSThread currentThread];
NSManagedObjectContext *privateContext = [[currentThread threadDictionary] objectForKey:@"managedObjectContext"];
request.entity = [NSEntityDescription entityForName:className inManagedObjectContext:privateContext];
request.predicate = [NSPredicate predicateWithFormat:@"id = %@", objectId];
NSError *executeFetchError = nil;
obj = [[privateContext executeFetchRequest:request error:&executeFetchError] lastObject];
if (executeFetchError) {
return NO;
} else if (!obj) {
return NO;
}else{
return YES;
}
}
@end