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) |
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
/* | |
* 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
/* | |
* 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
/* | |
* 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
/* | |
* 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
/* | |
* We can fetch a record, modify it and then call this save function to save our changes | |
*/ | |
- (BOOL)saveToDb{ | |
NSError *error = nil; | |
BOOL returnVal = ( [[self managedObjectContext] save:&error] ); | |
if(error != nil) | |
NSLog(@"%@", error); | |
return returnVal; | |
} |
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
// | |
// SingletonClass+ARC.h | |
// the smart engineer | |
// | |
// Copyright (c) 2013 Yasir M Turk. No rights reserved. | |
// | |
#import <foundation/Foundation.h> | |
@interface SingletonClass : NSObject { |
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
// | |
// YTCoreDataAdapter.h | |
// the smart engineer | |
// | |
// Copyright (c) 2013 Yasir M Turk. No rights reserved. | |
// | |
@interface YTCoreDataAdapter : NSObject { | |
} |
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
/* | |
* Log the complete ASIHTTPRequest HTTP Request | |
*/ | |
+ (void)logRequest:(ASIHTTPRequest *)request{ | |
NSLog(@"url:%@", request.url); | |
NSLog(@"method:%@", request.requestMethod); | |
NSLog(@"head:%@", request.requestHeaders); | |
if (request.postLength > 0) { | |
NSLog(@"body:%@", [[NSString alloc]initWithBytes:[request.postBody bytes] length:request.postLength encoding:NSUTF8StringEncoding]); | |
} |
OlderNewer