1

这是 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];
        }
    }
}
4

4 回答 4

1

问题是您有 2 个方法相互调用。该testMapping方法调用该reflexiveTestMapping方法由于该行:

[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"children" toKeyPath:@"children" withMapping:[MappingProvider reflexiveTestMapping]]];

由于该行,该reflexiveTestMapping方法调用该方法:testMapping

[mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:nil toKeyPath:@"name" withMapping:[MappingProvider testMapping]]];

这是一个硬循环,与映射没有直接关系,只是与您尝试创建映射的方式有关。

不需要以这种方式创建映射,因为childrenJSON 中的关系仅包含名称,而不包含完整对象。

于 2013-08-05T12:49:08.083 回答
0

这种递归的一个“解决方案”是根据估计的跳转来限制递归调用的最大数量,在这种情况下是两次跳转。

+ (RKMapping *)reflexiveTestMapping
{
    RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Test class]) inManagedObjectStore:[InfoDataModel sharedDataModel].objectStore];

    mapping.identificationAttributes = @[ @"name" ];

    // No more recursion calls from this point

    return mapping;
}
于 2013-08-05T11:50:21.923 回答
0

这就是我修复递归的方式:

+ (RKMapping *)testMapping
{
    RKEntityMapping *innerMapping = [RKEntityMapping mappingForEntityForName:NSStringFromClass([Test class]) inManagedObjectStore:[InfoDataModel sharedDataModel].objectStore];
    innerMapping.identificationAttributes = @[ @"name" ];

    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:innerMapping]];

    return mapping;
}
于 2013-08-05T15:18:42.740 回答
0

这就是我用 swift 所做的

class var keyAll: [String] {
    return [
      "title",
      "desc"
    ]
  }

  class var objectMapping: RKObjectMapping {
    let childMapping = RKObjectMapping(forClass: Category.self)
    childMapping.addAttributeMappingsFromArray(keyAll)

    let mapping = RKObjectMapping(forClass: Category.self)
    mapping.addAttributeMappingsFromArray(keyAll)

    mapping.addPropertyMapping(RKRelationshipMapping(fromKeyPath: keyChilds, toKeyPath: keyChilds, withMapping: childMapping))
    return mapping
  }
于 2016-05-17T04:41:28.940 回答