RestKit性能和核心数据

use*_*765 15 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"];
Run Code Online (Sandbox Code Playgroud)

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

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

// Option.optionType -> OptionType

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

// Option.unit -> Unit

[optionMapping addConnectionForRelationship:@"unit" connectedBy:@"unitID"];
Run Code Online (Sandbox Code Playgroud)

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

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

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];
Run Code Online (Sandbox Code Playgroud)

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

[_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];
                     }
                 }];
Run Code Online (Sandbox Code Playgroud)

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

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]
Run Code Online (Sandbox Code Playgroud)

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

小智 2

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

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