这是 RestKit 中的错误还是我没有正确配置它?使用以下映射时,我得到一个无限循环:
JSON:
[
{
"name": "Test 1",
"subtests": [
{
"name": "Subtest 1"
},
{
"name": "Subtest 2"
},
{
"name": "Subtest 3"
}
],
"children": ["Test 1"]
},
{
"name": "Test 2",
"subtests": [
{
"name": "Subtest 4"
},
{
"name": "Subtest 5"
},
{
"name": "Subtest 6"
}
],
"children": ["Test 2"]
}
]
映射提供者:
+ (RKMapping *)testMapping
{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Test class]) inManagedObjectStore:[InfoDataModel sharedDataModel].objectStore];
mapping.identificationAttributes = @[ @"name" ];
[mapping addAttributeMappingsFromArray:@[ @"name" ]];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"children" toKeyPath:@"children" withMapping:[MappingProvider reflexiveTestMapping]]];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"subtests" toKeyPath:@"subtests" withMapping:[MappingProvider subTestMapping]]];
return mapping;
}
+ (RKMapping *)reflexiveTestMapping
{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Test class]) inManagedObjectStore:[InfoDataModel sharedDataModel].objectStore];
mapping.identificationAttributes = @[ @"name" ];
[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"name" withMapping:[MappingProvider testMapping]]];
return mapping;
}
+ (RKMapping *)subTestMapping;
{
RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([SubTest class]) inManagedObjectStore:[InfoDataModel sharedDataModel].objectStore];
mapping.identificationAttributes = @[ @"name" ];
[mapping addAttributeMappingsFromArray:@[ @"name" ]];
return mapping;
}
我从这里调用它:
- (IBAction)loadTestAndSubtestEntity:(id)sender {
RKLogConfigureByName("RestKit", RKLogLevelWarning);
RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);
RKLogConfigureByName("RestKit/Network", RKLogLevelTrace);
NSString *filePathComponent = [self.pathComponent stringByAppendingPathComponent:@"test.json"];
[[InfoDataModel sharedDataModel] importDataFromJsonFilePathComponent:filePathComponent withMapping:[MappingProvider testMapping]];
}
由于挂起,因此不会生成任何输出:self.storePath 使用文件路径进行初始化。
- (void)importDataFromJsonFilePathComponent:(NSString *)jsonFilePathComponent withMapping:(RKMapping *)mapping
{
if (self.storePath) {
NSString *jsonFilePath = [RKApplicationDataDirectory() stringByAppendingPathComponent:jsonFilePathComponent];
MyLog(@"InfoDataModel. JSON file: %@", jsonFilePath);
RKManagedObjectImporter *importer = [[RKManagedObjectImporter alloc] initWithManagedObjectModel:self.objectStore.managedObjectModel storePath:self.storePath];
importer.resetsStoreBeforeImporting = NO;
MyLog(@"InfoDataModel. Store: %@", self.storePath);
NSError *error = nil;
[importer importObjectsFromItemAtPath:jsonFilePath
withMapping:mapping
keyPath:nil
error:&error];
BOOL success = [importer finishImporting:&error];
if (success) {
[importer logSeedingInfo];
}
}
}