2
votes

I'm trying to map a response from the server that have 2 properties:

  • A Number that is the count of the objets in the array
  • An array user objets that i already have mapped in other class.

It's how i have the model of the class:

enter image description here

and its related to the user class with the relationship "participants".

I know the nested class that i want to map is working because i use this in other classes and works great, but this implementation is different so i don't know how can i map this.

I have this in my classes:

.h Class:

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "NSManagedObject+Convenience.h"

@interface DUParticipant : NSManagedObject

@property (nonatomic, retain) NSNumber * participantsCount;
@property (nonatomic, retain) NSSet *participants;

+ (RKEntityMapping *)mapping;
+ (void)configMapping;

@end

.m Class

#import "DUParticipant.h"
#import "DUUser+Ext.h"

@implementation DUParticipant

@dynamic participantsCount;
@dynamic participants;

+ (RKEntityMapping *)mapping
{
    RKEntityMapping* map = [super mapping];

    [map addAttributeMappingsFromDictionary:@{
        @"participants_count"  : @"participantsCount"
     }];


    return map;
}

+(void) configMapping{


    RKObjectManager* man= [RKObjectManager sharedManager];
    RKEntityMapping* map = [self mapping];
    [man addResponseDescriptor:[RKResponseDescriptor responseDescriptorWithMapping:map pathPattern:@"/v1/users/participants" keyPath:nil
                                                                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]];

    RKObjectMapping *requestMapping = [RKObjectMapping requestMapping];
    [requestMapping addAttributeMappingsFromDictionary:@{
        @"participants_count"  : @"participantsCount"
     }];

    [map addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"participants"
                                                                        toKeyPath:@"participants"
                                                                      withMapping:[DUUser mapping]]];

    [man addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapping:requestMapping
                                                                    objectClass:[DUParticipant class] rootKeyPath:@"DUParticipant"]];

}

@end

And the response of the json is:

{
    "participants_count": 1,
    "participants": [
        {
            "participant": {
                "id": 3,
                "gender": "M",
                "name": "Oscar",
                "lastname": "Lares",
                "username": "CaLe",
                "email": "[email protected]",
                "bio": "UX Designer! Mac Geek, Computers, Gadgets and Movies lover!",
                "created_at": "2012-07-28T18:34:27Z",
                "updated_at": "2013-07-29T13:05:21Z",
                "cover": null,
                "avatar": {
                    "id": 108,
                    "original": "https://duuin.s3.amazonaws.com/uploads/image/file/108/photo.jpg",
                    "thumbnail": "https://duuin.s3.amazonaws.com/uploads/image/file/108/thumb_photo.jpg"
                }
            }
        }
    ]

**** ** EDIT** **** This is what i have in log console, it's the json response:

response.body = 
{
    "participants_count": 2,
    "participants": [
        {
            "participant": {
                "id": 3,
                "gender": "M",
                "name": "blabla",
                "lastname": "blabla",
                "username": "blabla",
                "email": "blabla",
                "bio": "blablablablablabla",
                "created_at": "2012-07-28T18:34:27Z",
                "updated_at": "2013-07-29T13:05:21Z",
                "cover": null,
                "avatar": {
                    "id": 108,
                    "original": "blabla",
                    "thumbnail": "blabla"
                }
            }
        },
        {
            "participant": {
                "id": 1,
                "gender": "M",
                "name": "blabla",
                "lastname": "blabla",
                "username": "blabla",
                "email": "blabla",
                "bio": "blabla",
                "created_at": "2012-07-22T20:30:18Z",
                "updated_at": "2013-07-31T03:52:26Z",
                "cover": {
                    "id": 21,
                    "original": "blabla",
                    "thumbnail": "blabla"
                },
                "avatar": {
                    "id": 19,
                    "original": "blabla",
                    "thumbnail": "blabla"
                }
            }
        }
    ]
}

And the Object in log:

2013-08-01 13:35:43.949 duuin[24550:c07] Participant: <NSManagedObject: 0x16d7ef00> (entity: DUParticipant; id: 0x16d74170 <x-coredata://1C844BC7-AF17-4D27-90F9-9F5B8ACDF848/DUParticipant/p86> ; data: {
    participants = nil;
    participantsCount = 2;
})
1
What is the result and have you turned on trace logging to see what's happening? - Wain
@Wain see the edit in the question bro - FxckDead

1 Answers

1
votes

I think the problem is with your nested class mapping. It may well work at other times but I guess the context is different. In your code shown the participants are inside a participant key inside a dictionary. This needs to be reflected in all of the keys in the mapping.


Presumably your mapping currently has:

@"gender" -> @"gender"

But for the JSON you show it needs to be:

@"participant.gender" -> @"gender"