RestKit性能和核心数据

时间:2013-08-01 23:26:38

标签: ios restkit

当只下载9,000个对象时,RestKit的性能会慢得令人无法接受 - 它在模拟器上花了10分钟,在iPad Mini上花了不确定的时间。

我从一个安静的界面下载了几个表。这一切都有效,但似乎存在指数尺度问题,当有许多对象时,这个问题变得无法忍受。下表遇到的问题最多:

optionMapping.identificationAttributes = @[@"optionID"];

// OptionType
RKEntityMapping *optionTypeMapping = [RKEntityMapping mappingForEntityForName:@"OptionType" inManagedObjectStore:rkMOS];
[optionTypeMapping addAttributeMappingsFromDictionary:@{
 @"id" : @"optionTypeID",
 @"option_type" : @"optionType",}];

optionTypeMapping.identificationAttributes = @[@"optionTypeID"];

属性optionTypeID在Core Data中编制索引,不是可选的。

与其他表有一些关系,我按如下方式映射:

// Option.optionType -> OptionType

[optionMapping addConnectionForRelationship:@"optionType" connectedBy: @"optionTypeID"];

// Option.unit -> Unit

[optionMapping addConnectionForRelationship:@"unit" connectedBy:@"unitID"];

这些似乎不是问题 - 我对它们进行了评论,下载仍然非常非常慢。

我设置了一个响应描述符(这是一个目录,所以我只需要下载它)。请注意,以下代码显示了所有表的设置。

NSArray *reqA = @[@{@"endpoint" : API_VENDORS_ENDPOINT,
                    @"mapping" : vendorMapping},

                  @{@"endpoint" : API_OPTION_TYPES_ENDPOINT,
                    @"mapping" : optionTypeMapping},

                  @{@"endpoint" : API_OPTIONS_ENDPOINT,
                    @"mapping" : optionMapping},

                  @{@"endpoint" : API_UNITS_ENDPOINT,
                    @"mapping" : unitMapping},

                  @{@"endpoint" : API_PRICE_TIERS_ENDPOINT,
                    @"mapping" : priceTierMapping},

                  @{@"endpoint" : API_PRODUCT_TYPES_ENDPOINT,
                    @"mapping" : productTypeMapping},

                  @{@"endpoint" : API_PRODUCTS_ENDPOINT,
                    @"mapping" : productMapping}
                  ];

for (NSDictionary *mapD in reqA) {

    RKResponseDescriptor *thisRD = [RKResponseDescriptor
                                    responseDescriptorWithMapping:[mapD valueForKey:@"mapping"]
                                    pathPattern:[mapD valueForKey:@"endpoint"]
                                    keyPath:nil
                                    statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [_objMgr addResponseDescriptor:thisRD];

我正在使用对象管理器下载表格:

[_objMgr getObjectsAtPath:verbStr
              parameters:nil
                 success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                     RKLogInfo(@"%@ load complete: %@", verbStr, [NSDate date]);

                     NSInteger idx = [loadA indexOfObject:verbStr] + 1;

                     if (idx < [loadA count]) {
                         [self load:[loadA objectAtIndex:idx] stack:loadA];
                     }

                     [[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:verbStr];
                     [[NSUserDefaults standardUserDefaults] synchronize];

                     NSDictionary *options = @{@"verb" : verbStr};
                     [[NSNotificationCenter defaultCenter] postNotificationName:OBJECTS_DOWNLOADED object:self userInfo:options];

                 } failure:^(RKObjectRequestOperation *operation, NSError *error) {
                     RKLogError(@"Load failed with error: %@", error);

                     NSInteger statusCode = operation.HTTPRequestOperation.response.statusCode;

                     if (401 == statusCode) {
                         [self resetAdmin];
                     }
                 }];

控制台输出显示映射正在进行,但它需要永远。以下只是数千个类似的片段:

2013-08-01 17:11:49.319 CarpetDirect[138:1507] D restkit.object_mapping:RKMappingOperation.m:952 Starting mapping operation...
2013-08-01 17:11:49.321 CarpetDirect[138:1507] T restkit.object_mapping:RKMappingOperation.m:953 Performing mapping operation: <RKMappingOperation 0x1dda2160> for 'Option' object. Mapping values from object {
   id = 1307;
   "option_type" = 0;
   unit = "<null>";
   value = "939 Puddle";
} to object <Option: 0x1dd55020> (entity: Option; id: 0x1dd55060 <x-coredata:///Option/t3ABD9C1C-1BBA-4C39-AEF7-EB3D1D9AFC0B1334> ; data: {
   optionID = 1307;
   optionType = nil;
   optionTypeID = 0;
   orderedItems =     (
);
   products =     (
);
   unit = nil;
   unitID = 0;
   value = nil;
}) with object mapping (null)
2013-08-01 17:11:49.324 CarpetDirect[138:1507] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'id' to 'optionID'
2013-08-01 17:11:49.326 CarpetDirect[138:1507] T restkit.object_mapping:RKMappingOperation.m:583 Skipped mapping of attribute value from keyPath 'id to keyPath 'optionID' -- value is unchanged (1307)
2013-08-01 17:11:49.329 CarpetDirect[138:1507] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'unit' to 'unitID'
2013-08-01 17:11:49.333 CarpetDirect[138:1507] T restkit.object_mapping:RKMappingOperation.m:431 Found transformable value at keyPath 'unit'. Transforming from type 'NSNull' to 'NSNumber'
2013-08-01 17:11:49.334 CarpetDirect[138:1507] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'unit' to 'unitID'. Value: (null)
2013-08-01 17:11:49.336 CarpetDirect[138:1507] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'value' to 'value'
2013-08-01 17:11:49.338 CarpetDirect[138:1507] T restkit.object_mapping:RKMappingOperation.m:572 Mapped attribute value from keyPath 'value' to 'value'. Value: 939 Puddle
2013-08-01 17:11:49.339 CarpetDirect[138:1507] T restkit.object_mapping:RKMappingOperation.m:550 Mapping attribute value keyPath 'option_type' to 'optionTypeID'
2013-08-01 17:11:49.342 CarpetDirect[138:1507] T restkit.object_mapping:RKMappingOperation.m:583 Skipped mapping of attribute value from keyPath 'option_type to keyPath 'optionTypeID' -- value is unchanged (0)
2013-08-01 17:11:49.345 CarpetDirect[138:1507] D restkit.object_mapping:RKMappingOperation.m:1021 Finished mapping operation successfully...
2013-08-01 17:11:49.348 CarpetDirect[138:1507]

关于如何加快速度的任何想法?我想要做的就是将数据从服务器发送到Core Data。

1 个答案:

答案 0 :(得分:2)

如果没有更多的上下文,很难说很多,但你的RestKit日志选项是什么?根据我的经验,RestKit中的映射日志记录非常冗长,速度降低了10倍。

禁用所有RestKit日志记录,并查看是否有任何改进。然后,如果仍然存在问题,请使用Instruments来分析您的应用程序 - 您应该很容易看到哪些代码路径占用了大部分时间(解析,RestKit映射,核心数据等)。