-
Notifications
You must be signed in to change notification settings - Fork 0
/
MisoDBLayerGeneric.m
80 lines (60 loc) · 2.24 KB
/
MisoDBLayerGeneric.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
//
// MisoDBLayerGeneric.m
// MisoUIV3
//
// Created by HEENA RASTOGI on 3/1/12.
// Copyright (c) 2012 Miso Media. All rights reserved.
//
#import "MisoDBLayerGeneric.h"
@implementation MisoDBLayerGeneric
+(NSArray*)fetchManagedObjectsFromEntityWithName:(NSString*)entityName
withinContext:(NSManagedObjectContext*)context
sortedBy:(NSArray*) sortDescriptors
filteredBy:(NSPredicate*)predicate
{
[sortDescriptors retain];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSError *error;
if(sortDescriptors)
[fetchRequest setSortDescriptors:sortDescriptors];
if(predicate)
[fetchRequest setPredicate:predicate];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
[sortDescriptors release];
[fetchRequest release];
return fetchedObjects;
}
+(NSError*)deleteManagedObjects:(NSArray*) managedObjectsArray
withinContext:(NSManagedObjectContext*) context
{
NSError* error = nil;
for(NSManagedObject* obj in managedObjectsArray)
{
[context deleteObject:obj];
}
if(![context save:&error])
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
return error;
}
+(NSError*)insertManagedObjects:(NSArray*) managedObjArr
withinContext:(NSManagedObjectContext*) context
{
for(NSManagedObject* obj in managedObjArr)
[context insertObject:obj];
NSError* error = nil;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
return error;
}
+(NSManagedObject*) allocEntityTypeObj:(NSString*) entityName
withinContext:(NSManagedObjectContext*) context
{
NSEntityDescription* entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
NSManagedObject* entity_obj = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:nil];
return entity_obj;
}
@end