문제

I've got a problem with empty json list mapping, I've got a JSON response that look this way :

{
    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" =             (
        );
    }

The mapping for the class look like this :

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

The mapping for the Restriction class :

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

No matter what I try I keep getting these annoying errors :

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" =     (
    );
})

What I'd like to do is map these empty list with an empty NSArray. Does anyone have a clue how to do that ? is it even possible ?

도움이 되었습니까?

해결책

The problem is that you have made the relationships non-optional. By default they will be empty sets (not arrays), but if you try to save when the sets are empty you will get validation errors because you configured that the relationships have to have at least one connection (that's what non-optional means).

If you want to allow zero connections, make the relationships optional.

Note that you might wan't to make the other end of the relationships non-optional, so you know if you have a GeographicalArea or TimeSlot then it must be connected to a Restriction. This would usually tie in with a Cascade deletion rule.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top