Skip to content

Instantly share code, notes, and snippets.

View yasirmturk's full-sized avatar
💻
the smart engineer

Yasir Türk yasirmturk

💻
the smart engineer
View GitHub Profile
@yasirmturk
yasirmturk / gist:1408335
Created November 30, 2011 07:27
A generic core data function that can query given entity with the given condition & return sorted results
/*
* 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)
@yasirmturk
yasirmturk / gist:1408338
Created November 30, 2011 07:31
A generic core data function that inserts a new row for the given entity & fill that row with given key/values
/*
* 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]];
@yasirmturk
yasirmturk / gist:1408341
Created November 30, 2011 07:33
A generic core data function that deletes a given entity
/*
* 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];
@yasirmturk
yasirmturk / gist:1408346
Created November 30, 2011 07:37
A generic core data function to check whether a record already exists
/*
* 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);
@yasirmturk
yasirmturk / gist:1408349
Created November 30, 2011 07:38
A generic core data function to Get a record based on the criteria
/*
* 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)
@yasirmturk
yasirmturk / gist:1408350
Created November 30, 2011 07:39
A generic core data function to Get all records
/*
* Get all records
*/
-(NSArray*)getAllRecordsFromEntity:(NSString *)entity{
NSArray *arr = [self queryDBForEntity:entity predicate:nil sortByField:nil];
//NSLog(@"%@", arr);
return arr;
}
@yasirmturk
yasirmturk / gist:1408352
Created November 30, 2011 07:40
A generic core data function to save and commit our changes to DB
/*
* 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;
}
@yasirmturk
yasirmturk / SingletonClass+ARC.h
Created March 5, 2013 09:03
Singleton classes is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. This idea is used throughout the iPhone SDK, for example, UIApplication has a method called sharedApplication which when called from anywhere will return the UI…
//
// SingletonClass+ARC.h
// the smart engineer
//
// Copyright (c) 2013 Yasir M Turk. No rights reserved.
//
#import <foundation/Foundation.h>
@interface SingletonClass : NSObject {
//
// YTCoreDataAdapter.h
// the smart engineer
//
// Copyright (c) 2013 Yasir M Turk. No rights reserved.
//
@interface YTCoreDataAdapter : NSObject {
}
@yasirmturk
yasirmturk / HTTPLog.m
Last active December 21, 2015 10:38
Log the complete ASIHTTPRequest HTTP Request
/*
* 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]);
}