Skip to content

Instantly share code, notes, and snippets.

@mwhuss
Created May 23, 2012 02:22
Show Gist options
  • Save mwhuss/2772874 to your computer and use it in GitHub Desktop.
Save mwhuss/2772874 to your computer and use it in GitHub Desktop.
Core Data class that makes common tasks easy

CMDManagedObject

This class helps perform common Core Data tasks like fetching and counting objects by removing the need to worry about entity descriptions, manually creating fetch requests, and handling context interactions for you.

Installation

  • Add GCMDManagedObject.h, GCMDManagedObject.m and CoreData.framework to your project
  • Make every object in your model inherit from an abstract GCManagedObject object
  • Add a date attribute called createdAt to your GCManagedObject object
  • Make all of your managed object source files inherit from CGManagedObject
//
// CMDManagedObject.h
//
// Created by Caleb Davenport on 3/215/11.
// Copyright (c) 2012 Caleb Davenport.
//
#import <CoreData/CoreData.h>
@interface CMDManagedObject : NSManagedObject
@property (nonatomic, retain) NSDate *createdAt;
// override this method to provide a model name other than the class name
+ (NSString *)modelName;
// create object in specified context
+ (id)instanceInContext:(NSManagedObjectContext *)context;
// create a fecth request based on the class
+ (NSFetchRequest *)fetchRequestInContext:(NSManagedObjectContext *)context;
// find objects in specified context
+ (NSArray *)allInContext:(NSManagedObjectContext *)context;
+ (NSArray *)allInContext:(NSManagedObjectContext *)context sortDescriptor:(NSSortDescriptor *)descriptor;
+ (NSArray *)allInContext:(NSManagedObjectContext *)context sortDescriptors:(NSArray *)descriptors;
+ (NSArray *)allInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate;
+ (NSArray *)allInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate sortDescriptor:(NSSortDescriptor *)descriptor;
+ (NSArray *)allInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)descriptors;
// count objects in specified context
+ (NSUInteger)countInContext:(NSManagedObjectContext *)context;
+ (NSUInteger)countInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate;
@end
//
// CMDManagedObject.m
//
// Created by Caleb Davenport on 3/215/11.
// Copyright (c) 2012 Caleb Davenport.
//
#import "CMDManagedObject.h"
@implementation CMDManagedObject
@dynamic createdAt;
#pragma mark - custom model name
+ (NSString *)modelName {
return NSStringFromClass(self);
}
#pragma mark - create objects
+ (id)instanceInContext:(NSManagedObjectContext *)context {
NSString *name = [self modelName];
id instance = [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:context];
[(id)instance setCreatedAt:[NSDate date]];
return instance;
}
#pragma mark - fetch requests
+ (NSFetchRequest *)fetchRequestInContext:(NSManagedObjectContext *)context {
NSString *name = [self modelName];
NSEntityDescription *entity = [NSEntityDescription entityForName:name inManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
#if __has_feature(objc_arc)
return request;
#else
return [request autorelease];
#endif
}
#pragma mark - find objects
+ (NSArray *)allInContext:(NSManagedObjectContext *)context {
return [self allInContext:context predicate:nil sortDescriptors:nil];
}
+ (NSArray *)allInContext:(NSManagedObjectContext *)context sortDescriptor:(NSSortDescriptor *)descriptor {
return [self allInContext:context predicate:nil sortDescriptor:descriptor];
}
+ (NSArray *)allInContext:(NSManagedObjectContext *)context sortDescriptors:(NSArray *)descriptors {
return [self allInContext:context predicate:nil sortDescriptors:descriptors];
}
+ (NSArray *)allInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate {
return [self allInContext:context predicate:predicate sortDescriptors:nil];
}
+ (NSArray *)allInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate sortDescriptor:(NSSortDescriptor *)descriptor {
return [self allInContext:context predicate:predicate sortDescriptors:[NSArray arrayWithObject:descriptor]];
}
+ (NSArray *)allInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate sortDescriptors:(NSArray *)descriptors {
NSFetchRequest *request = [self fetchRequestInContext:context];
if (descriptors) {
[request setSortDescriptors:descriptors];
}
if (predicate) {
[request setPredicate:predicate];
}
return [context executeFetchRequest:request error:nil];
}
#pragma mark - count objects
+ (NSUInteger)countInContext:(NSManagedObjectContext *)context {
return [self countInContext:context predicate:nil];
}
+ (NSUInteger)countInContext:(NSManagedObjectContext *)context predicate:(NSPredicate *)predicate {
NSFetchRequest *request = [self fetchRequestInContext:context];
if (predicate) {
[request setPredicate:predicate];
}
return [context countForFetchRequest:request error:nil];
}
@end
Copyright (c) 2012 Caleb Davenport.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment