Skip to content

Instantly share code, notes, and snippets.

@sdpjswl
Created April 18, 2016 03:10
Show Gist options
  • Select an option

  • Save sdpjswl/e6470bfc5c3cf8e3ba666ac42e7038bb to your computer and use it in GitHub Desktop.

Select an option

Save sdpjswl/e6470bfc5c3cf8e3ba666ac42e7038bb to your computer and use it in GitHub Desktop.
Helper methods for working with Core Data
//
// NSManagedObjectContext+Convenience.h
// ResidentPortal
//
// Created by Prashant Patil on 18/06/14.
// Copyright (c) 2014 Property Solutions. All rights reserved.
//
#import <CoreData/CoreData.h>
#import <Foundation/Foundation.h>
@interface NSManagedObjectContext (Convenience)
@end
@interface NSArray (CDArrayExtensions)
- (id)firstObject;
@end
@interface NSManagedObjectContext (DictionaryExtensions)
- (NSMutableDictionary*) mutableDictionaryForEntityWithName:(NSString*)entityName keyedBy:(NSString*)keyName;
@end
@interface NSManagedObjectContext(insert)
/*
This very short little category allows me to insert new objects into a context by simply doing this:
[context insertNewEntityWityName:[entity name]];
*/
-(NSManagedObject *) insertNewEntityWithName:(NSString *)name;
@end
@interface NSManagedObjectContext (Entities)
- (NSSet *)CfetchObjectsForEntityName:(NSString *)newEntityName
withPredicate:(id)stringOrPredicate, ...;
/*
Almost all methods here have comparable syntax.
It's all something like '...entitiyWithName: whereKey: like:',
which works just the way it says
*/
//returns the number of entities with a certain name
- (int)numberOfEntitiesWithName:(NSString *)entityName;
//returns the number of entities of 'entityName' where 'key' has a certain 'value'
- (int)numberOfEntitiesWithName:(NSString *)entityName where:(NSString *)key like:(NSString *)value;
//returns all entities with a certain name
- (NSArray *)entitiesWithName:(NSString *)entityName;
- (NSArray *)entitiesWithName:(NSString *)entityName predicate:(NSPredicate*)predicate;
//returns all entities of 'entityName' where 'key' has a certain 'value'
- (NSArray *)entitiesWithName:(NSString *)entityName where:(NSString *)key like:(NSString *)value;
//returns all entities of 'entityName' where 'key' contains string (case insensitive)
- (NSArray *)entitiesWithName:(NSString *)entityName whereKey:(NSString *)key contains:(NSString *)value;
// returns all entities of 'entityName' where 'key' is one of a collection of values (collection must be an array, set, or dictionary (in which case the values are used))
- (NSArray*)entitiesWithName: (NSString*)entityName whereKey: (NSString*)key isIn: (id)collection;
//returns YES if there exists an entity with 'entityName' which has a 'key' with a certain 'value', NO otherwise
- (BOOL)entityWithNameExists:(NSString *)entityName whereKey:(NSString *)key like:(NSString *)value;
//Containing strings (case insensitive)
- (BOOL)entityWithNameExists:(NSString *)entityName whereKey:(NSString *)key contains:(NSString *)value;
//Case Insensitive optional
- (BOOL)entityWithNameExists:(NSString *)entityName whereKey:(NSString *)key like:(NSString *)value caseInsensitive:(BOOL)flag;
//exact object matches
- (BOOL)entityWithNameExists:(NSString *)entityName whereKey:(NSString *)key equalToObject:(id )value;
//returns an entity with 'entityName' with 'key' set to 'value'
- (id)entityWithName:(NSString *)entityName whereKey:(NSString *)key like:(NSString *)value;
//Containing strings (case insensitive)
- (id)entityWithName:(NSString *)entityName whereKey:(NSString *)key contains:(NSString *)value;
//Case Insensitive optional
- (id)entityWithName:(NSString *)entityName whereKey:(NSString *)key like:(NSString *)value caseInsensitive:(BOOL)flag;
//exact matches (not strings)
- (id)entityWithName:(NSString *)entityName whereKey:(NSString *)key equalToObject:(id )value;
//creates an entity with an unique value for key, derived from the default value.
//for example, if there already exists and entity "Person" with "name"="Foo", it will create an entity with "name"="Foo 1" etc
- (id)createEntity:(NSString *)entityName withUniqueValueForKey:(NSString *)key defaultValue:(NSString *)def;
//tries to retrieve an entity with certain characteristics and if it fails, it creates one
- (id)retrieveOrCreateEntityWithName:(NSString *)entityName whereKey:(NSString *)key like:(NSString *)value;
//exact object matches
- (id)retrieveOrCreateEntityWithName:(NSString *)entityName whereKey:(NSString *)key equalToObject:(id )value;
/*
Help moving between contexts
*/
- (NSArray*)objectsWithObjectIDs:(NSArray*)originalObjectIDs;
- (NSArray*)objectsWithObjectsFromOtherContext:(NSArray*)originalObjects;
- (NSManagedObject*)objectWithObjectFromOtherContext:(NSManagedObject*)originalObject;
#pragma mark -
#pragma mark Fetch all unsorted
/** @brief Convenience method to fetch all objects for a given Entity name in
* this context.
*
* The objects are returned in the order specified by Core Data.
*/
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName;
#pragma mark -
#pragma mark Fetch all sorted
/** @brief Convenience method to fetch all objects for a given Entity name in
* the context.
*
* The objects are returned in the order specified by the provided key and
* order.
*/
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortByKey:(NSString*)key
ascending:(BOOL)ascending;
/** @brief Convenience method to fetch all objects for a given Entity name in
* the context.
*
* If the sort descriptors array is not nil, the objects are returned in the
* order specified by the sort descriptors. Otherwise, the objects are returned
* in the order specified by Core Data.
*/
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortWith:(NSArray*)sortDescriptors;
#pragma mark -
#pragma mark Fetch filtered unsorted
/** @brief Convenience method to fetch selected objects for a given Entity name
* in the context.
*
* If the predicate is not nil, the selection is filtered by the provided
* predicate.
*
* The objects are returned in the order specified by Core Data.
*/
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
withPredicate:(NSPredicate*)predicate;
/** @brief Convenience method to fetch selected objects for a given Entity name
* in the context.
*
* The selection is filtered by the provided formatted predicate string and
* arguments.
*
* The objects are returned in the order specified by Core Data.
*/
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
predicateWithFormat:(NSString*)predicateFormat, ...;
#pragma mark -
#pragma mark Fetch filtered sorted
/** @brief Convenience method to fetch selected objects for a given Entity name
* in the context.
*
* If the predicate is not nil, the selection is filtered by the provided
* predicate.
*
* The objects are returned in the order specified by the provided key and
* order.
*/
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortByKey:(NSString*)key
ascending:(BOOL)ascending
withPredicate:(NSPredicate*)predicate;
/** @brief Convenience method to fetch selected objects for a given Entity name
* in the context.
*
* If the predicate is not nil, the selection is filtered by the provided
* predicate.
*/
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortWith:(NSArray*)sortDescriptors
withPredicate:(NSPredicate*)predicate;
/** @brief Convenience method to fetch selected objects for a given Entity name
* in the context.
*
* The selection is filtered by the provided formatted predicate string and
* arguments.
*
* The objects are returned in the order specified by the provided key and
* order.
*/
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortByKey:(NSString*)key
ascending:(BOOL)ascending
predicateWithFormat:(NSString*)predicateFormat, ...;
/** @brief Convenience method to fetch selected objects for a given Entity name
* in the context.
*
* The selection is filtered by the provided formatted predicate string and
* arguments.
*
* If the sort descriptors array is not nil, the objects are returned in the
* order specified by the sort descriptors. Otherwise, the objects are returned
* in the order specified by Core Data.
*/
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortWith:(NSArray*)sortDescriptors
predicateWithFormat:(NSString*)predicateFormat, ...;
@end
//
// NSManagedObjectContext+Convenience.m
// ResidentPortal
//
// Created by Prashant Patil on 18/06/14.
// Copyright (c) 2014 Property Solutions. All rights reserved.
//
#import "NSManagedObjectContext+Convenience.h"
@implementation NSManagedObjectContext (Convenience)
@end
@implementation NSManagedObjectContext (insert)
-(NSManagedObject *) insertNewEntityWithName:(NSString *)name
{
return [NSEntityDescription insertNewObjectForEntityForName:name inManagedObjectContext:self];
}
@end
@interface NSManagedObjectContext (EntitiesPrivate)
//case insensitive
- (BOOL)entityWithNameExists:(NSString *)entityName whereKey:(NSString *)key caseInsensitiveLike:(NSString *)value;
- (id)entityWithName:(NSString *)entityName whereKey:(NSString *)key caseInsensitiveLike:(NSString *)value;
//------
@end
@implementation NSManagedObjectContext (EntitiesPrivate)
#pragma mark -
#pragma mark Case Insensitive Always
- (BOOL)entityWithNameExists:(NSString *)entityName whereKey:(NSString *)key caseInsensitiveLike:(NSString *)value{
return [self entityWithName:entityName whereKey:key caseInsensitiveLike:value] != nil;
}
- (id)entityWithName:(NSString *)entityName whereKey:(NSString *)key caseInsensitiveLike:(NSString *)value
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like[c] %@",key , value];
[request setPredicate:predicate];
return [[self executeFetchRequest:request error:NULL] firstObject];
}
@end
@implementation NSManagedObjectContext (Entities)
// Convenience method to fetch the array of objects for a given Entity
// name in the context, optionally limiting by a predicate or by a predicate
// made from a format NSString and variable arguments.
//
- (NSSet *)CfetchObjectsForEntityName:(NSString *)newEntityName
withPredicate:(id)stringOrPredicate, ...
{
NSEntityDescription *entity = [NSEntityDescription
entityForName:newEntityName inManagedObjectContext:self];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
if (stringOrPredicate)
{
NSPredicate *predicate;
if ([stringOrPredicate isKindOfClass:[NSString class]])
{
va_list variadicArguments;
va_start(variadicArguments, stringOrPredicate);
predicate = [NSPredicate predicateWithFormat:stringOrPredicate
arguments:variadicArguments];
va_end(variadicArguments);
}
else
{
NSAssert2([stringOrPredicate isKindOfClass:[NSPredicate class]],
@"Second parameter passed to %s is of unexpected class %@",
sel_getName(_cmd), [[stringOrPredicate class] description]);
predicate = (NSPredicate *)stringOrPredicate;
}
[request setPredicate:predicate];
}
NSError *error = nil;
NSArray *results = [self executeFetchRequest:request error:&error];
if (error != nil)
{
[NSException raise:NSGenericException format:@"%@", error.description];
}
return [NSSet setWithArray:results];
}
//Containing strings
- (BOOL)entityWithNameExists:(NSString *)entityName whereKey:(NSString *)key contains:(NSString *)value{
return [self entityWithName:entityName whereKey:key contains:value] != nil;
}
- (id)entityWithName:(NSString *)entityName whereKey:(NSString *)key contains:(NSString *)value{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K contains[c] %@",key , value];
[request setPredicate:predicate];
return [[self executeFetchRequest:request error:NULL] firstObject];
}
- (NSArray*)entitiesWithName:(NSString *)entityName whereKey:(NSString *)key contains:(NSString *)value{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self]];
if ((nil != key)&&(nil != value)) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K contains[c] %@",key , value];
[request setPredicate:predicate];
}
// predicate to limit size
return [self executeFetchRequest:request error:NULL];
}
#pragma mark -
#pragma mark Case Insensitive Optional
- (BOOL)entityWithNameExists:(NSString *)entityName whereKey:(NSString *)key like:(NSString *)value caseInsensitive:(BOOL)flag{
if(flag)
return [self entityWithNameExists:entityName whereKey:key caseInsensitiveLike:value];
else
return [self entityWithNameExists:entityName whereKey:key like:value];
}
- (id)entityWithName:(NSString *)entityName whereKey:(NSString *)key like:(NSString *)value caseInsensitive:(BOOL)flag{
if(flag)
return [self entityWithName:entityName whereKey:key caseInsensitiveLike:value];
else
return [self entityWithName:entityName whereKey:key like:value];
}
#pragma mark -
#pragma mark Equality
- (BOOL)entityWithNameExists:(NSString *)entityName whereKey:(NSString *)key equalToObject:(id )value{
return [self entityWithName:entityName whereKey:key equalToObject:value] != nil;
}
- (id)entityWithName: (NSString*)entityName whereKey: (NSString*)key equalToObject: (id)value
{
NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity: [NSEntityDescription entityForName: entityName inManagedObjectContext: self]];
NSPredicate* predicate = [NSPredicate predicateWithFormat: @"%K == %@", key, value]; //[NSPredicate predicateWithFormat:@"%K == %@", key, value];
[request setFetchLimit: 1];
[request setPredicate: predicate];
// [predicate release];
// predicate = nil;
NSArray* fetchResults = [self executeFetchRequest: request error: NULL];
request = nil;
//return [[self executeFetchRequest:request error:NULL] firstObject];
return [fetchResults firstObject];
}
- (id)retrieveOrCreateEntityWithName:(NSString *)entityName whereKey:(NSString *)key equalToObject:(id )value
{
id obj = [self entityWithName:entityName whereKey:key equalToObject:value];
if (!obj) {
obj = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self];
[obj setValue:value forKey:key];
}
return obj;
}
#pragma mark -
- (BOOL)entityWithNameExists:(NSString *)entityName whereKey:(NSString *)key like:(NSString *)value
{
return [self entityWithName:entityName whereKey:key like:value] != nil;
}
- (id)entityWithName:(NSString *)entityName whereKey:(NSString *)key like:(NSString *)value
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",key , value];
[request setPredicate:predicate];
return [[self executeFetchRequest:request error:NULL] firstObject];
}
- (NSArray*)entitiesWithName: (NSString*)entityName whereKey: (NSString*)key isIn: (id)collection
{
NSArray* result = nil;
NSFetchRequest* request = [[NSFetchRequest alloc] init];
//debugLog(@"searching for entities with name %@ where %@ is in %@ (length %d)", entityName, key, collection, [collection count]);
[request setEntity: [NSEntityDescription entityForName: entityName inManagedObjectContext: self]];
[request setFetchLimit: [collection count]];
//[request setResultType: NSManagedObjectIDResultType];
NSPredicate* predicate = [NSPredicate predicateWithFormat: @"%K in %@", key, collection];
[request setPredicate: predicate];
result = [self executeFetchRequest: request error: NULL];
request = nil;
return result;
}
- (id)retrieveOrCreateEntityWithName:(NSString *)entityName whereKey:(NSString *)key like:(NSString *)value
{
id obj = [self entityWithName:entityName whereKey:key like:value];
if (!obj) {
obj = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self];
[obj setValue:value forKey:key];
}
return obj;
}
- (id)createEntity:(NSString *)entityName withUniqueValueForKey:(NSString *)key defaultValue:(NSString *)def
{
NSManagedObject *object = [NSEntityDescription insertNewObjectForEntityForName:entityName
inManagedObjectContext:self];
NSString *baseName = def;
NSString *suggestedName = baseName;
int counter = 1;
//find a unique name for the New Collection
BOOL go = YES;
while (go) {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self]];
[request setPredicate:[NSPredicate predicateWithFormat:@"%K like %@",key,suggestedName]];
NSInteger count = [self countForFetchRequest:request error:NULL];
if (count != 0) {
suggestedName = [NSString stringWithFormat:@"%@ %i",baseName,counter];
counter++;
} else
go = NO;
}
[object setValue:suggestedName forKey:key];
return object;
}
- (int)numberOfEntitiesWithName:(NSString *)entityName
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self]];
return (int)[self countForFetchRequest:request error:NULL];
}
- (NSArray *)entitiesWithName:(NSString *)entityName predicate:(NSPredicate*)predicate{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self]];
[request setPredicate:predicate];
return [self executeFetchRequest:request error:NULL];
}
- (NSArray *)entitiesWithName:(NSString *)entityName
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self]];
return[self executeFetchRequest:request error:NULL];
}
- (int)numberOfEntitiesWithName:(NSString *)entityName where:(NSString *)key like:(NSString *)value
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",key , value];
[request setPredicate:predicate];
return (int)[self countForFetchRequest:request error:NULL];
}
- (NSArray *)entitiesWithName:(NSString *)entityName where:(NSString *)key like:(NSString *)value
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:entityName inManagedObjectContext:self]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%K like %@",key , value];
[request setPredicate:predicate];
return [self executeFetchRequest:request error:NULL];
}
- (NSArray*)objectsWithObjectIDs:(NSArray*)originalObjectIDs
{
NSMutableArray* newObjects = [NSMutableArray arrayWithCapacity:[originalObjectIDs count]];
[originalObjectIDs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSManagedObject* newObj = [self objectWithID:(NSManagedObjectID*)obj];
[newObjects addObject:newObj];
}];
return newObjects;
}
- (NSArray*)objectsWithObjectsFromOtherContext:(NSArray*)originalObjects{
NSMutableArray* newObjects = [NSMutableArray arrayWithCapacity:[originalObjects count]];
[originalObjects enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSManagedObjectID* anID = [(NSManagedObject*)obj objectID];
NSManagedObject* newObj = [self objectWithID:anID];
[newObjects addObject:newObj];
}];
return newObjects;
}
- (NSManagedObject*)objectWithObjectFromOtherContext:(NSManagedObject*)originalObject{
NSManagedObjectID* anID = [(NSManagedObject*)originalObject objectID];
NSManagedObject* newObj = [self objectWithID:anID];
return newObj;
}
#pragma mark -
#pragma mark Fetch all unsorted
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
{
return [self fetchObjectsForEntityName:entityName sortWith:nil
withPredicate:nil];
}
#pragma mark -
#pragma mark Fetch all sorted
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortByKey:(NSString*)key
ascending:(BOOL)ascending
{
return [self fetchObjectsForEntityName:entityName sortByKey:key
ascending:ascending withPredicate:nil];
}
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortWith:(NSArray*)sortDescriptors
{
return [self fetchObjectsForEntityName:entityName sortWith:sortDescriptors
withPredicate:nil];
}
#pragma mark -
#pragma mark Fetch filtered unsorted
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
withPredicate:(NSPredicate*)predicate
{
return [self fetchObjectsForEntityName:entityName sortWith:nil
withPredicate:predicate];
}
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
predicateWithFormat:(NSString*)predicateFormat, ...
{
va_list variadicArguments;
va_start(variadicArguments, predicateFormat);
NSPredicate* predicate = [NSPredicate predicateWithFormat:predicateFormat
arguments:variadicArguments];
va_end(variadicArguments);
return [self fetchObjectsForEntityName:entityName sortWith:nil
withPredicate:predicate];
}
#pragma mark -
#pragma mark Fetch filtered sorted
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortByKey:(NSString*)key
ascending:(BOOL)ascending
withPredicate:(NSPredicate*)predicate
{
NSSortDescriptor* sort = [[NSSortDescriptor alloc] initWithKey:key
ascending:ascending];
#if !__has_feature(objc_arc)
[sort autorelease];
#endif
return [self fetchObjectsForEntityName:entityName sortWith:[NSArray
arrayWithObject:sort] withPredicate:predicate];
}
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortWith:(NSArray*)sortDescriptors
withPredicate:(NSPredicate*)predicate
{
NSEntityDescription* entity = [NSEntityDescription entityForName:entityName
inManagedObjectContext:self];
NSFetchRequest* request = [[NSFetchRequest alloc] init];
#if !__has_feature(objc_arc)
[request autorelease];
#endif
[request setEntity:entity];
if (predicate)
{
[request setPredicate:predicate];
}
if (sortDescriptors)
{
[request setSortDescriptors:sortDescriptors];
}
NSError* error = nil;
NSArray* results = [self executeFetchRequest:request error:&error];
if (error != nil)
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
[NSException raise:NSGenericException format:@"%@", [error description]];
}
return results;
}
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortByKey:(NSString*)key
ascending:(BOOL)ascending
predicateWithFormat:(NSString*)predicateFormat, ...
{
va_list variadicArguments;
va_start(variadicArguments, predicateFormat);
NSPredicate* predicate = [NSPredicate predicateWithFormat:predicateFormat
arguments:variadicArguments];
va_end(variadicArguments);
return [self fetchObjectsForEntityName:entityName sortByKey:key
ascending:ascending withPredicate:predicate];
}
- (NSArray*)fetchObjectsForEntityName:(NSString*)entityName
sortWith:(NSArray*)sortDescriptors
predicateWithFormat:(NSString*)predicateFormat, ...
{
va_list variadicArguments;
va_start(variadicArguments, predicateFormat);
NSPredicate* predicate = [NSPredicate predicateWithFormat:predicateFormat
arguments:variadicArguments];
va_end(variadicArguments);
return [self fetchObjectsForEntityName:entityName sortWith:sortDescriptors
withPredicate:predicate];
}
@end
@implementation NSArray (CDArrayExtensions)
- (id)firstObject
{
if ([self count] > 0)
return [self objectAtIndex:0];
return nil;
}
@end
@implementation NSManagedObjectContext (DictionaryExtensions)
- (NSMutableDictionary*) mutableDictionaryForEntityWithName:(NSString*)entityName keyedBy:(NSString*)keyName
{
NSArray* entities = [self entitiesWithName:entityName];
NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithCapacity:[entities count]];
for( NSManagedObject* obj in entities )
{
[dict setObject:obj forKey:[obj valueForKey:keyName]];
}
return dict;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment