Rework RKManagedObjectMappingOperationDataSource to avoid performing mutable(Array|Set|OrderedSet)ValueForKeyPath: unless there are existing objects and the identificationAttributes are nil. refs #1232

This commit is contained in:
Blake Watters
2013-03-10 14:22:38 -04:00
parent 2581d21762
commit e62a9862d7
7 changed files with 329 additions and 76 deletions

View File

@@ -17,7 +17,7 @@
#import "RKHuman.h"
#import "RKChild.h"
#import "RKParent.h"
//#import "RKBenchmark.h"
#import "RKBenchmark.h"
@interface RKManagedObjectMappingOperationDataSourceTest : RKTestCase
@@ -1143,78 +1143,77 @@
expect([human isDeleted]).to.equal(YES);
}
// TODO: Import bencharmk utility somehow...
//- (void)testMappingAPayloadContainingRepeatedObjectsPerformsAcceptablyWithFetchRequestMappingCache
//{
// RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
// RKFetchRequestManagedObjectCache *managedObjectCache = [RKFetchRequestManagedObjectCache new];
// RKManagedObjectMappingOperationDataSource *mappingOperationDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext
// cache:managedObjectCache];
// managedObjectStore.managedObjectCache = managedObjectCache;
//
// RKEntityMapping *childMapping = [RKEntityMapping mappingForEntityForName:@"Child" inManagedObjectStore:managedObjectStore];
// childMapping.identificationAttributes = @[ @"childID" ];
// [childMapping addAttributeMappingsFromArray:@[@"name", @"childID"]];
//
// RKEntityMapping *parentMapping = [RKEntityMapping mappingForEntityForName:@"Parent" inManagedObjectStore:managedObjectStore];
// [parentMapping addAttributeMappingsFromArray:@[@"parentID", @"name"]];
// parentMapping.identificationAttributes = @[ @"parentID" ];
// [parentMapping addRelationshipMappingWithSourceKeyPath:@"children" mapping:childMapping];
//
//
// NSDictionary *mappingsDictionary = @{ @"parents": parentMapping };
// NSDictionary *JSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"benchmark_parents_and_children.json"];
// RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithObject:JSON mappingsDictionary:mappingsDictionary];
// mapper.mappingOperationDataSource = mappingOperationDataSource;
//
// RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelOff);
// RKLogConfigureByName("RestKit/CoreData", RKLogLevelOff);
//
// [RKBenchmark report:@"Mapping with Fetch Request Cache" executionBlock:^{
// for (NSUInteger i = 0; i < 50; i++) {
// [mapper performMapping];
// }
// }];
// NSUInteger parentCount = [managedObjectStore.persistentStoreManagedObjectContext countForEntityForName:@"Parent" predicate:nil error:nil];
// NSUInteger childrenCount = [managedObjectStore.persistentStoreManagedObjectContext countForEntityForName:@"Child" predicate:nil error:nil];
// assertThatInteger(parentCount, is(equalToInteger(25)));
// assertThatInteger(childrenCount, is(equalToInteger(51)));
//}
//
//- (void)testMappingAPayloadContainingRepeatedObjectsPerformsAcceptablyWithInMemoryMappingCache
//{
// RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
// RKInMemoryManagedObjectCache *managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
// RKManagedObjectMappingOperationDataSource *mappingOperationDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext
// cache:managedObjectCache];
// managedObjectStore.managedObjectCache = managedObjectCache;
//
// RKEntityMapping *childMapping = [RKEntityMapping mappingForEntityForName:@"Child" inManagedObjectStore:managedObjectStore];
// childMapping.identificationAttributes = @[ @"childID" ];
// [childMapping addAttributeMappingsFromArray:@[@"name", @"childID"]];
//
// RKEntityMapping *parentMapping = [RKEntityMapping mappingForEntityForName:@"Parent" inManagedObjectStore:managedObjectStore];
// [parentMapping addAttributeMappingsFromArray:@[@"parentID", @"name"]];
// parentMapping.identificationAttributes = @[ @"parentID" ];
// [parentMapping addRelationshipMappingWithSourceKeyPath:@"children" mapping:childMapping];
//
// NSDictionary *mappingsDictionary = @{ @"parents": parentMapping };
// NSDictionary *JSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"benchmark_parents_and_children.json"];
// RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithObject:JSON mappingsDictionary:mappingsDictionary];
// mapper.mappingOperationDataSource = mappingOperationDataSource;
// RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelOff);
// RKLogConfigureByName("RestKit/CoreData", RKLogLevelOff);
//
// [RKBenchmark report:@"Mapping with In Memory Cache" executionBlock:^{
// for (NSUInteger i = 0; i < 50; i++) {
// [mapper performMapping];
// }
// }];
// NSUInteger parentCount = [managedObjectStore.persistentStoreManagedObjectContext countForEntityForName:@"Parent" predicate:nil error:nil];
// NSUInteger childrenCount = [managedObjectStore.persistentStoreManagedObjectContext countForEntityForName:@"Child" predicate:nil error:nil];
// assertThatInteger(parentCount, is(equalToInteger(25)));
// assertThatInteger(childrenCount, is(equalToInteger(51)));
//}
- (void)testMappingAPayloadContainingRepeatedObjectsPerformsAcceptablyWithFetchRequestMappingCache
{
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
RKFetchRequestManagedObjectCache *managedObjectCache = [RKFetchRequestManagedObjectCache new];
RKManagedObjectMappingOperationDataSource *mappingOperationDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext
cache:managedObjectCache];
managedObjectStore.managedObjectCache = managedObjectCache;
RKEntityMapping *childMapping = [RKEntityMapping mappingForEntityForName:@"Child" inManagedObjectStore:managedObjectStore];
childMapping.identificationAttributes = @[ @"childID" ];
[childMapping addAttributeMappingsFromArray:@[@"name", @"childID"]];
RKEntityMapping *parentMapping = [RKEntityMapping mappingForEntityForName:@"Parent" inManagedObjectStore:managedObjectStore];
[parentMapping addAttributeMappingsFromArray:@[@"parentID", @"name"]];
parentMapping.identificationAttributes = @[ @"parentID" ];
[parentMapping addRelationshipMappingWithSourceKeyPath:@"children" mapping:childMapping];
NSDictionary *mappingsDictionary = @{ @"parents": parentMapping };
NSDictionary *JSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"benchmark_parents_and_children.json"];
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:JSON mappingsDictionary:mappingsDictionary];
mapper.mappingOperationDataSource = mappingOperationDataSource;
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelOff);
RKLogConfigureByName("RestKit/CoreData", RKLogLevelOff);
[RKBenchmark report:@"Mapping with Fetch Request Cache" executionBlock:^{
for (NSUInteger i = 0; i < 50; i++) {
[mapper start];
}
}];
NSUInteger parentCount = [managedObjectStore.persistentStoreManagedObjectContext countForEntityForName:@"Parent" predicate:nil error:nil];
NSUInteger childrenCount = [managedObjectStore.persistentStoreManagedObjectContext countForEntityForName:@"Child" predicate:nil error:nil];
assertThatInteger(parentCount, is(equalToInteger(25)));
assertThatInteger(childrenCount, is(equalToInteger(51)));
}
- (void)testMappingAPayloadContainingRepeatedObjectsPerformsAcceptablyWithInMemoryMappingCache
{
RKManagedObjectStore *managedObjectStore = [RKTestFactory managedObjectStore];
RKInMemoryManagedObjectCache *managedObjectCache = [[RKInMemoryManagedObjectCache alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext];
RKManagedObjectMappingOperationDataSource *mappingOperationDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext
cache:managedObjectCache];
managedObjectStore.managedObjectCache = managedObjectCache;
RKEntityMapping *childMapping = [RKEntityMapping mappingForEntityForName:@"Child" inManagedObjectStore:managedObjectStore];
childMapping.identificationAttributes = @[ @"childID" ];
[childMapping addAttributeMappingsFromArray:@[@"name", @"childID"]];
RKEntityMapping *parentMapping = [RKEntityMapping mappingForEntityForName:@"Parent" inManagedObjectStore:managedObjectStore];
[parentMapping addAttributeMappingsFromArray:@[@"parentID", @"name"]];
parentMapping.identificationAttributes = @[ @"parentID" ];
[parentMapping addRelationshipMappingWithSourceKeyPath:@"children" mapping:childMapping];
NSDictionary *mappingsDictionary = @{ @"parents": parentMapping };
NSDictionary *JSON = [RKTestFixture parsedObjectWithContentsOfFixture:@"benchmark_parents_and_children.json"];
RKMapperOperation *mapper = [[RKMapperOperation alloc] initWithRepresentation:JSON mappingsDictionary:mappingsDictionary];
mapper.mappingOperationDataSource = mappingOperationDataSource;
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelOff);
RKLogConfigureByName("RestKit/CoreData", RKLogLevelOff);
[RKBenchmark report:@"Mapping with In Memory Cache" executionBlock:^{
for (NSUInteger i = 0; i < 50; i++) {
[mapper start];
}
}];
NSUInteger parentCount = [managedObjectStore.persistentStoreManagedObjectContext countForEntityForName:@"Parent" predicate:nil error:nil];
NSUInteger childrenCount = [managedObjectStore.persistentStoreManagedObjectContext countForEntityForName:@"Child" predicate:nil error:nil];
assertThatInteger(parentCount, is(equalToInteger(25)));
assertThatInteger(childrenCount, is(equalToInteger(51)));
}
- (void)testMappingIdentificationAttributesFromElementsOnAnArray
{