Skip to content

Instantly share code, notes, and snippets.

@yasirmturk
Created March 5, 2013 09:30
Show Gist options
  • Save yasirmturk/5089060 to your computer and use it in GitHub Desktop.
Save yasirmturk/5089060 to your computer and use it in GitHub Desktop.
//
// YTCoreDataAdapter.h
// the smart engineer
//
// Copyright (c) 2013 Yasir M Turk. No rights reserved.
//
@interface YTCoreDataAdapter : NSObject {
}
@property (retain, nonatomic) NSManagedObjectContext *managedObjectContext;
+ (DBAdapter *)create;
- (NSArray *)queryDBForEntity:(NSString *)entityName predicate:(NSPredicate *)predicate sortByField:(NSString *)sortField;
- (BOOL)deleteObject:(NSManagedObject*)obj;
- (BOOL)saveToDB;
@end
//
// YTCoreDataAdapter.m
// the smart engineer
//
// Copyright (c) 2013 Yasir M Turk. No rights reserved.
//
#import "YTCoreDataAdapter.h"
#import "AppDelegate.h"
@implementation YTCoreDataAdapter
/*
* Gets NSManagedObjectContext from application delegate and returns an autorelease object
*/
+ (YTCoreDataAdapter *)create{
YTCoreDataAdapter *db = [[YTCoreDataAdapter alloc]init];
if (db) {
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
db.managedObjectContext = [appDelegate managedObjectContext];
}
return [db autorelease];
}
/*
* 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){
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortField ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
}
if(predicate != nil)
[request setPredicate:predicate];
NSError *error;
NSArray *fetchResults = [[self managedObjectContext] executeFetchRequest:request error:&error];
[request release];
if (fetchResults == nil) {
NSLog(@"Error fetching result %@", [error description]);
}
return fetchResults;
}
/*
* Delete given entity object from DB
*/
- (BOOL)deleteObject:(NSManagedObject*)obj{
NSError *error;
if(obj != nil){
[self.managedObjectContext deleteObject:obj];
if( [self.managedObjectContext save:&error] )
return YES;
}
return NO;
}
/*
* Commit changes to DB
*/
- (BOOL)saveToDB{
BOOL returnVal = YES;
NSError *error = nil;
if (self.managedObjectContext != nil){
if ([self.managedObjectContext hasChanges] && ![self.managedObjectContext save:&error]){
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
//abort();
returnVal = NO;
}
}
return returnVal;
}
- (void)dealloc{
[managedObjectContext release];
[super dealloc];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment