سؤال

I'm sending out a request to get an object with an nested array of data. However, RestKit is only saving the last item in the nested array of data.

For example, here's the JSON response the server is delivering

{
    "nutrition_day": {
        "id": "5342e9e13138610012420100",
        "start_on": "2014-04-07",
        "meal_goals": [
            {
                "id": "5342e9e13138610012410100",
                "name": "Breakfast",
                "food_entries": [
                    {
                        "id": "535429513663320008210000",
                        "consumed_on": "2014-04-20",
                        "food_id": 33801,
                        "serving_id": 29457,
                    }
                ]
            },
            {
                "id": "5342e9e13138610012430100",
                "name": "Morning Snack",
                "food_entries": []
            }
        ]
    }
}

However, using the code below, when I attempt to access nutrition_data.meal_goals, I am left with only the last item in the array "Morning Snack" and am not getting "Breakfast"

Mapping:

+ (void)mapping
{
    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
    RKEntityMapping *responseMapping = [RKEntityMapping mappingForEntityForName:@"NutritionDay" inManagedObjectStore:[[RKObjectManager sharedManager] managedObjectStore]];

    [requestMapping addAttributeMappingsFromArray:@[@"date"]];

    [responseMapping addAttributeMappingsFromArray:@[@"start_on", @"protein_target"]];
    [responseMapping setIdentificationAttributes:@[@"start_on"]];
    [responseMapping addRelationshipMappingWithSourceKeyPath:@"meal_goals" mapping:[MealGoal responseMapping]];

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:requestMapping
                                                                                   objectClass:[NutritionDay class]
                                                                                   rootKeyPath:@"nutrition_day"
                                                                                        method:RKRequestMethodGET];

    RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:responseMapping
                                                                                            method:RKRequestMethodAny
                                                                                       pathPattern:@"nutrition/days/:date"
                                                                                           keyPath:@"nutrition_day"
                                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

    [[RKObjectManager sharedManager] addRequestDescriptor:requestDescriptor];
    [[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
}

Request:

[[RKObjectManager sharedManager] getObjectsAtPath:@"nutrition/days/2014-12-12" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NutritionDay *nut = ((NutritionDay *)mappingResult.firstObject);
    NSLog(@"%lu", (long)nut.meal_goals.count); // this always returns 1 when it should be 2
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"fail");
}];

Is there something I need to do so that RestKit recognizes the entire array of the relationship and saves it?

هل كانت مفيدة؟

المحلول

I would suggest that you should include the id in your mapping and use that as the identification attribute.

meal_goals should be a to-many relationship (with inverse).

Your log should be:

NSLog(@"%lu", (long)nut.meal_goals.count);

Other than those things, the code you show looks correct. Log the entire mapping result to verify the contents.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top