Eliminated the use of the ActiveRecord pattern across the library.

This commit is contained in:
Blake Watters
2012-07-19 13:34:32 -04:00
parent ae8b98ac1a
commit e952f451fb
41 changed files with 912 additions and 1867 deletions

View File

@@ -8,6 +8,8 @@
#import <objc/runtime.h>
#import "NSManagedObjectContext+RKAdditions.h"
#import "NSEntityDescription+RKAdditions.h"
#import "RKLog.h"
static char NSManagedObject_RKManagedObjectStoreAssociatedKey;
@@ -23,4 +25,51 @@ static char NSManagedObject_RKManagedObjectStoreAssociatedKey;
objc_setAssociatedObject(self, &NSManagedObject_RKManagedObjectStoreAssociatedKey, managedObjectStore, OBJC_ASSOCIATION_ASSIGN);
}
- (id)insertNewObjectForEntityForName:(NSString *)entityName
{
return [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:self];
}
- (NSUInteger)countForEntityForName:(NSString *)entityName predicate:(NSPredicate *)predicate error:(NSError **)error
{
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:entityName];
fetchRequest.predicate = predicate;
return [self countForFetchRequest:fetchRequest error:error];
}
- (id)fetchObjectForEntity:(NSEntityDescription *)entity withValueForPrimaryKeyAttribute:(id)primaryKeyValue
{
NSPredicate *predicate = [entity predicateForPrimaryKeyAttributeWithValue:primaryKeyValue];
if (! predicate) {
RKLogWarning(@"Attempt to fetchObjectForEntity for entity with nil primaryKeyAttribute. Set the primaryKeyAttributeName and try again! %@", self);
return nil;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest new] autorelease];
fetchRequest.entity = entity;
fetchRequest.predicate = predicate;
fetchRequest.fetchLimit = 1;
__block NSError *error;
__block NSArray *objects;
[self performBlockAndWait:^{
objects = [self executeFetchRequest:fetchRequest error:&error];
}];
if (! objects) {
RKLogCoreDataError(error);
return nil;
}
if ([objects count] == 1) {
return objects[0];
}
return nil;
}
- (id)fetchObjectForEntityForName:(NSString *)entityName withValueForPrimaryKeyAttribute:(id)primaryKeyValue
{
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:self];
return [self fetchObjectForEntity:entity withValueForPrimaryKeyAttribute:primaryKeyValue];
}
@end