This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Get all records | |
*/ | |
-(NSArray*)getAllRecordsFromEntity:(NSString *)entity{ | |
NSArray *arr = [self queryDBForEntity:entity predicate:nil sortByField:nil]; | |
//NSLog(@"%@", arr); | |
return arr; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Get a record based on the criteria | |
*/ | |
- (id)getRecordFromEntity:(NSString *)entity whereField:(NSString *)fieldName isEqualTo:(id)val | |
{ | |
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(%K = %@)", fieldName, val]; | |
//NSLog(@"(P: %@)", [pred predicateFormat]); | |
NSArray *arr = [self queryDBForEntity:entity predicate:pred sortByField:fieldName]; | |
//NSLog(@"%@", arr); | |
if ([arr count] > 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Useful function to check whether a record already exists | |
*/ | |
-(BOOL)doesRecordExistForEntity:(NSString *)entity fieldToCheck:(NSString *)fieldName valueToMatch:(id)val | |
{ | |
NSPredicate *pred = [NSPredicate predicateWithFormat:@"(%K = %@)", fieldName, val]; | |
//NSLog(@"(P: %@)", [pred predicateFormat]); | |
NSArray *arr = [self queryDBForEntity:entity predicate:pred sortByField:fieldName]; | |
//NSLog(@"%@", arr); | |
return ([arr count] > 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Given an object delete it | |
* | |
* @param obj: obj to delete from database | |
* @returns: true if successful in deleting else returns false | |
*/ | |
- (BOOL)deleteObject:(id)obj{ | |
NSError *error; | |
if(obj != nil){ | |
[[self managedObjectContext] deleteObject:obj]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A generic function that inserts a new row for the given entity and fill that row with given key/values | |
*/ | |
-(void)insertObjectForEntity:(NSString *)entityName objectsToInsert:(NSDictionary *)dictObjects | |
{ | |
NSError *error; | |
NSManagedObject *objToInsert = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:[self managedObjectContext]]; | |
for (id key in [dictObjects allKeys]) { | |
[objToInsert setValue:key forKey:[dictObjects objectForKey:key]]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* A generic function that can query that given entity with the given condition and return sorted results. | |
*/ | |
-(NSArray *)queryDBForEntity:(NSString *)entityName predicate:(NSPredicate *)predicate sortByField:(NSString *)sortField | |
{ | |
NSFetchRequest *request = [[NSFetchRequest alloc] init]; | |
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]]; | |
[request setEntity:entity]; | |
if(sortField != nil) |
NewerOlder