Skip to content

Instantly share code, notes, and snippets.

@ktusznio
Created July 16, 2012 18:26
Show Gist options
  • Save ktusznio/3124141 to your computer and use it in GitHub Desktop.
Save ktusznio/3124141 to your computer and use it in GitHub Desktop.
Core Data Categories to get started with Core Data
//
// NSManagedObject+KTManagedObject.h
// Created by Kamil Tusznio on 12-07-10.
//
#import <CoreData/CoreData.h>
@interface NSManagedObject (KTManagedObject)
+ (NSArray *)fetchAll;
+ (id)newRecord;
@end
//
// NSManagedObject+KTManagedObject.m
// Created by Kamil Tusznio on 12-07-10.
//
#import "NSManagedObject+KTManagedObject.h"
@implementation NSManagedObject (KTManagedObject)
+ (NSArray *)fetchAll {
NSError *error;
NSString *entityName = NSStringFromClass([self class]);
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName
inManagedObjectContext:[NSManagedObjectContext sharedInstance]];
[fetchRequest setEntity:entity];
NSArray *records = [[NSManagedObjectContext sharedInstance] executeFetchRequest:fetchRequest
error:&error];
if (error) {
NSLog(@"Error fetching %@: %@", entityName, [error description]);
}
return records;
}
+ (id)newRecord {
NSString *entityName = NSStringFromClass([self class]);
NSManagedObjectContext *context = [NSManagedObjectContext sharedInstance];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entityName
inManagedObjectContext:context];
// Insert the entity into the context and save it.
id record = [[[self class] alloc] initWithEntity:entityDescription
insertIntoManagedObjectContext:context];
[context save];
return record;
}
@end
//
// NSManagedObjectContext+SharedInstance.h
// Created by Kamil Tusznio on 12-07-05.
//
#import <CoreData/CoreData.h>
@interface NSManagedObjectContext (SharedInstance)
+ (NSManagedObjectContext *)sharedInstance;
- (BOOL)save;
@end
//
// NSManagedObjectContext+SharedInstance.m
// Created by Kamil Tusznio on 12-07-05.
//
#import "NSManagedObjectContext+SharedInstance.h"
#import <objc/runtime.h>
#import "AppDelegate.h"
@implementation NSManagedObjectContext (SharedInstance)
+ (NSManagedObjectContext *)sharedInstance {
if (objc_getAssociatedObject(self, @"sharedInstance") == nil) {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
objc_setAssociatedObject(self, @"sharedInstance", [appDelegate managedObjectContext], OBJC_ASSOCIATION_RETAIN);
}
return (NSManagedObjectContext *)objc_getAssociatedObject(self, @"sharedInstance");
}
- (BOOL)save {
NSError *error;
BOOL result = [self save:&error];
if (error) {
NSLog(@"Error saving context %@", [error description]);
}
return result;
}
@end
//
// NSManagedObjectContext+SharedInstance.h
// Created by Kamil Tusznio on 12-07-05.
//
#import <CoreData/CoreData.h>
@interface NSManagedObjectContext (SharedInstance)
+ (NSManagedObjectContext *)sharedInstance;
- (BOOL)save;
@end
//
// NSManagedObjectContext+SharedInstance.m
// Created by Kamil Tusznio on 12-07-05.
//
#import "NSManagedObjectContext+SharedInstance.h"
#import <objc/runtime.h>
#import "AppDelegate.h"
@implementation NSManagedObjectContext (SharedInstance)
+ (NSManagedObjectContext *)sharedInstance {
if (objc_getAssociatedObject(self, @"sharedInstance") == nil) {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
objc_setAssociatedObject(self, @"sharedInstance", [appDelegate managedObjectContext], OBJC_ASSOCIATION_RETAIN);
}
return (NSManagedObjectContext *)objc_getAssociatedObject(self, @"sharedInstance");
}
- (BOOL)save {
NSError *error;
BOOL result = [self save:&error];
if (error) {
NSLog(@"Error saving context %@", [error description]);
}
return result;
}
@end
//
// NSManagedObject+KTManagedObject.h
// Created by Kamil Tusznio on 12-07-10.
//
#import <CoreData/CoreData.h>
@interface NSManagedObject (KTManagedObject)
+ (NSArray *)fetchAll;
+ (id)newRecord;
@end
//
// NSManagedObject+KTManagedObject.m
// Created by Kamil Tusznio on 12-07-10.
//
#import "NSManagedObject+KTManagedObject.h"
@implementation NSManagedObject (KTManagedObject)
+ (NSArray *)fetchAll {
NSError *error;
NSString *entityName = NSStringFromClass([self class]);
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName
inManagedObjectContext:[NSManagedObjectContext sharedInstance]];
[fetchRequest setEntity:entity];
NSArray *records = [[NSManagedObjectContext sharedInstance] executeFetchRequest:fetchRequest
error:&error];
if (error) {
NSLog(@"Error fetching %@: %@", entityName, [error description]);
}
return records;
}
+ (id)newRecord {
NSString *entityName = NSStringFromClass([self class]);
NSManagedObjectContext *context = [NSManagedObjectContext sharedInstance];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entityName
inManagedObjectContext:context];
// Insert the entity into the context and save it.
id record = [[[self class] alloc] initWithEntity:entityDescription
insertIntoManagedObjectContext:context];
[context save];
return record;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment