kampfgnu
kampfgnu

Reputation: 107

RestKit ios nested objects of same type

how can i map nested objects of the same type? i have an xml with objects that may contain multiple objects of the same type:

<entry location="l1">
    <entry location="l1.1">
        <entry location="l1.1.1">
        </entry>
        <entry location="l1.1.2">
        </entry>
    </entry>
</entry>

i get an infinite recursion if i add a propertymapping with the same mapping:

+ (RKObjectMapping *)objectMapping {
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Entry class]];
    [mapping addAttributeMappingsFromDictionary:@{@"location": @"location"}];
    [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"nextEntries"
                                                                            toKeyPath:@"entries"
                                                                          withMapping:[Entry objectMapping]]];

    return mapping;
}

is it possible to add the sub-objects to an array of each parent object?

cheers

//edit: following code works for me:

+ (RKObjectMapping *)objectMapping
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Entry class]];
    [mapping addAttributeMappingsFromDictionary:@{@"location": @"location"}];

    RKObjectMapping *innerMapping = [RKObjectMapping mappingForClass:[Entry class]];
    [innerMapping addAttributeMappingsFromDictionary:@{@"location": @"location"}];

    [mapping addPropertyMapping:[RKRelationshipMapping
        relationshipMappingFromKeyPath:@"entry"
        toKeyPath:@"entries"
        withMapping:innerMapping]];

    return mapping;
}

Upvotes: 0

Views: 348

Answers (1)

Wain
Wain

Reputation: 119031

I haven't tried it so I don't know if it will work but try this:

+ (RKObjectMapping *)objectMapping
{
    RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Entry class]];
    [mapping addAttributeMappingsFromDictionary:@{@"location": @"location"}];

    RKObjectMapping *innerMapping = [RKObjectMapping mappingForClass:[Entry class]];
    [innerMapping addAttributeMappingsFromDictionary:@{@"location": @"location"}];

    [mapping addPropertyMapping:[RKRelationshipMapping
        relationshipMappingFromKeyPath:@"nextEntries"
        toKeyPath:@"entries"
        withMapping:innerMapping]];

    return mapping;
}

Not sure that toKeyPath:@"entries" is correct, it may need to be toKeyPath:@"entry" based on your XML.

The infinite recursion is because you're calling the same method from itself ([Entry objectMapping]), it has nothing to do with the mapping as such.

Upvotes: 2

Related Questions