Created
July 16, 2012 18:26
-
-
Save ktusznio/3124141 to your computer and use it in GitHub Desktop.
Core Data Categories to get started with Core Data
This file contains hidden or 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
// | |
// NSManagedObject+KTManagedObject.h | |
// Created by Kamil Tusznio on 12-07-10. | |
// | |
#import <CoreData/CoreData.h> | |
@interface NSManagedObject (KTManagedObject) | |
+ (NSArray *)fetchAll; | |
+ (id)newRecord; | |
@end |
This file contains hidden or 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
// | |
// 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 |
This file contains hidden or 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
// | |
// NSManagedObject+KTManagedObject.h | |
// Created by Kamil Tusznio on 12-07-10. | |
// | |
#import <CoreData/CoreData.h> | |
@interface NSManagedObject (KTManagedObject) | |
+ (NSArray *)fetchAll; | |
+ (id)newRecord; | |
@end |
This file contains hidden or 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
// | |
// 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