mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-04-24 04:46:01 +08:00
Eliminated the use of the ActiveRecord pattern across the library.
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user