0

我遇到了空 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;
}

限制类的映射:

    +(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 映射这些空列表。有谁知道如何做到这一点?有可能吗?

4

1 回答 1

1

问题是您已经使关系成为非可选的。默认情况下,它们将是空集(不是数组),但是如果您尝试在集为空时保存,则会收到验证错误,因为您配置了关系必须至少有一个连接(这就是非可选的意思)。

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

请注意,您可能不希望使关系的另一端成为非可选的,因此您知道是否有 aGeographicalAreaTimeSlot然后它必须连接到 a Restriction。这通常与Cascade删除规则相关联。

于 2013-07-08T11:28:58.223 回答