RestKit 仅保存响应数组中的最后一项

Posted

技术标签:

【中文标题】RestKit 仅保存响应数组中的最后一项【英文标题】:RestKit only saving last item in array of response 【发布时间】:2014-04-22 22:27:40 【问题描述】:

我正在发送一个请求以获取一个包含嵌套数据数组的对象。但是,RestKit 只保存嵌套数据数组中的最后一项。

例如,这是服务器正在传递的 JSON 响应


    "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": []
            
        ]
    

但是,使用下面的代码,当我尝试访问 Nutrition_data.meal_goals 时,我只剩下数组“Morning Snack”中的最后一项并且没有得到“Breakfast”

映射:

+ (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];

请求:

[[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");
];

我需要做些什么来让 RestKit 识别整个关系数组并保存它吗?

【问题讨论】:

【参考方案1】:

我建议您在映射中包含id 并将其用作标识属性。

meal_goals 应该是一对多关系(逆向)。

你的日志应该是:

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

除此之外,您显示的代码看起来是正确的。记录整个映射结果以验证内容。

【讨论】:

以上是关于RestKit 仅保存响应数组中的最后一项的主要内容,如果未能解决你的问题,请参考以下文章

如何仅定位 v-for 数组中的最后一项?

当响应包含在数组中时,RestKit POSTed 托管对象变得重复

Javascript 仅设置数组中最后一项的属性

MagicalRecord 导入仅保存最后一项

PHP Session仅保存最后一项[重复]

在 Postgres 9.4 中查找 JSON 数组中的最后一项