mirror of
https://github.com/zhigang1992/RestKit.git
synced 2026-05-14 16:19:26 +08:00
Integrated primaryKey extension to NSEntityDescription and refactored cache strategy
classes to eliminate issues with duplicated objects. closes #611, #612, #613, #618 * NSEntityDescription is now aware of the primaryKeyAttribute. Can be configured via Interface Builder within Xcode or programatically. * Added findByPrimaryKey: interface to the Core Data extensions. * Relaxed dependencies on RKManagedObjectMapping across the system now that primaryKey is available without a reference to the mapping.
This commit is contained in:
44
Code/CoreData/RKFetchRequestManagedObjectCache.m
Normal file
44
Code/CoreData/RKFetchRequestManagedObjectCache.m
Normal file
@@ -0,0 +1,44 @@
|
||||
//
|
||||
// RKFetchRequestMappingCache.m
|
||||
// RestKit
|
||||
//
|
||||
// Created by Jeff Arena on 1/24/12.
|
||||
// Copyright (c) 2012 RestKit. All rights reserved.
|
||||
//
|
||||
|
||||
#import "RKFetchRequestManagedObjectCache.h"
|
||||
#import "NSManagedObject+ActiveRecord.h"
|
||||
#import "NSEntityDescription+RKAdditions.h"
|
||||
#import "RKLog.h"
|
||||
|
||||
// Set Logging Component
|
||||
#undef RKLogComponent
|
||||
#define RKLogComponent lcl_cRestKitCoreData
|
||||
|
||||
@implementation RKFetchRequestManagedObjectCache
|
||||
|
||||
- (NSManagedObject *)findInstanceOfEntity:(NSEntityDescription *)entity
|
||||
withPrimaryKeyAttribute:(NSString *)primaryKeyAttribute
|
||||
value:(id)primaryKeyValue
|
||||
inManagedObjectContext:(NSManagedObjectContext *)managedObjectContext
|
||||
{
|
||||
NSAssert(entity, @"Cannot find existing managed object without a target class");
|
||||
NSAssert(primaryKeyAttribute, @"Cannot find existing managed object instance without mapping that defines a primaryKeyAttribute");
|
||||
NSAssert(primaryKeyValue, @"Cannot find existing managed object by primary key without a value");
|
||||
NSAssert(managedObjectContext, @"Cannot find existing managed object with a context");
|
||||
|
||||
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
|
||||
[fetchRequest setEntity:entity];
|
||||
[fetchRequest setFetchLimit:1];
|
||||
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"%K = %@", primaryKeyAttribute, primaryKeyValue]];
|
||||
NSArray *objects = [NSManagedObject executeFetchRequest:fetchRequest];
|
||||
RKLogDebug(@"Found objects '%@' using fetchRequest '%@'", objects, fetchRequest);
|
||||
|
||||
NSManagedObject *object = nil;
|
||||
if ([objects count] > 0) {
|
||||
object = [objects objectAtIndex:0];
|
||||
}
|
||||
return object;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user