Restkit映射空json列表与关系

时间:2013-07-08 10:48:24

标签: core-data mapping restkit empty-list

我的空json列表映射存在问题,我有一个看起来像这样的JSON响应:

{
    id = 1;
    image = "http://image.com/image.png";
    name = "Test Category #1";
    "restricted_position" = 0;
    "restricted_time" = 0;
    restrictions =         {
        "restricted_position_detail" =             (
        );
        "restricted_time_detail" =             (
        );
    }

该类的映射如下所示:

    +(RKManagedObjectMapping *)getObjectMapping
{
    RKObjectRelationshipMapping *restrictionMapping = [RKObjectRelationshipMapping mappingFromKeyPath:@"restrictions" toKeyPath:@"restrictions" withMapping:[Restriction getObjectMapping]];

    RKManagedObjectMapping *mapping = [RKManagedObjectMapping mappingForClass:[PLCategory class] inManagedObjectStore:[RKObjectManager sharedManager].objectStore];
    [mapping mapKeyPath:@"id" toAttribute:@"identifiant"];
    [mapping mapKeyPath:@"name" toAttribute:@"name"];
    [mapping mapKeyPath:@"image" toAttribute:@"imageUrl"];
    [mapping mapKeyPath:@"restricted_position" toAttribute:@"restricted_position"];
    [mapping mapKeyPath:@"restricted_time" toAttribute:@"restricted_time"];

    [mapping addRelationshipMapping:restrictionMapping];
    [mapping setPrimaryKeyAttribute:@"identifiant"];

    return mapping;
}

Restriction类的映射:

    +(RKManagedObjectMapping *)getObjectMapping
{
    RKObjectRelationshipMapping *zg_mapping = [RKObjectRelationshipMapping mappingFromKeyPath:@"restricted_time_detail" toKeyPath:@"restricted_time_detail" withMapping:[GeographicalArea getObjectMapping]];
    RKObjectRelationshipMapping *ph_mapping = [RKObjectRelationshipMapping mappingFromKeyPath:@"restricted_position_detail" toKeyPath:@"restricted_position_detail" withMapping:[TimeSlot getObjectMapping]];
    RKManagedObjectMapping *mapping = [RKManagedObjectMapping mappingForClass:[Restriction class] inManagedObjectStore:[RKObjectManager sharedManager].objectStore];
    [mapping addRelationshipMapping:zg_mapping];
    [mapping addRelationshipMapping:ph_mapping];

    return mapping;
}

无论我尝试什么,我都会不断收到这些恼人的错误:

Core Data Save Error
                               NSLocalizedDescription:      The operation couldn’t be completed. (Cocoa error 1580.)
                               NSValidationErrorKey:            restricted_position_detail
                               NSValidationErrorPredicate:  (null)
                               NSValidationErrorObject:
<Restriction: 0xb9c2f20> (entity: Restriction; id: 0xb9b5d90 <x-coredata:///Restriction/tE6CDEA6C-5A2A-48AE-9454-A8E6CC35F5078> ; data: {
    category = "0xb9b67b0 <x-coredata:///PLCategory/tE6CDEA6C-5A2A-48AE-9454-A8E6CC35F5077>";
    "restricted_position_detail" =     (
    );
    "restricted_time_detail" =     (
    );
})
2013-07-08 12:39:00.716 [7442:28513] E restkit.core_data:RKManagedObjectStore.m:263 Core Data Save Error
                               NSLocalizedDescription:      The operation couldn’t be completed. (Cocoa error 1580.)
                               NSValidationErrorKey:            restricted_time_detail
                               NSValidationErrorPredicate:  (null)
                               NSValidationErrorObject:
<Restriction: 0xb9c2f20> (entity: Restriction; id: 0xb9b5d90 <x-coredata:///Restriction/tE6CDEA6C-5A2A-48AE-9454-A8E6CC35F5078> ; data: {
    category = "0xb9b67b0 <x-coredata:///PLCategory/tE6CDEA6C-5A2A-48AE-9454-A8E6CC35F5077>";
    "restricted_position_detail" =     (
    );
    "restricted_time_detail" =     (
    );
})

我想做的是将这些空列表与空NSArray映射。有谁知道如何做到这一点?它甚至可能吗?

1 个答案:

答案 0 :(得分:1)

问题是您已将关系设为非可选。默认情况下,它们将是空集(不是数组),但如果您尝试在集合为空时进行保存,则会出现验证错误,因为您已配置关系必须至少具有一个连接(这是非可选方式)。

如果要允许零连接,请将关系设为可选。

请注意,您可能不希望将关系的另一端设为非可选,因此您知道如果您有GeographicalAreaTimeSlot,那么它必须连接到{{1 }}。这通常会与Restriction删除规则相关联。