iosobjective-crestkitrestkit-0.20

Restkit - Relationship mapping not working


I'm experiencing Restkit, using the version 0.25.0. I followed exactly what the documentation says for the relationship mapping, but for some reasons, I have an empty mapping result ! But when I remove the relationship object (data), I have a mapping result !!! But of course, the mapping result doesn't contain the data I need, the one I added as relationship. I followed the example they have in this link :
https://github.com/RestKit/RestKit/wiki/Object-mapping#relationships

When I print the JSON with the debugger, here's the output :

{
   "status": "success",
   "data": {
       "id": 11,
       "provider": "email",
       "uid": "riri@gmail.com",
       "name": null,
       "nickname": null,
       "image": null,
       "email": "riri@gmail.com",
       "country": "United States",
       "city": "Milan, Metropolitan City of Milan, Italy",
       "gender": "m",
       "birthday": "2015-06-25"
   }
}

Here's the code how I make the request :

RKObjectManager *manager = [RKObjectManager sharedManager];

RKLogConfigureByName("RestKit/ObjectMapping", RKLogLevelTrace);

RKObjectMapping *jsonResponseMapping = [JSONResponse mappingObject];
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:jsonResponseMapping
                                                                                        method:RKRequestMethodAny
                                                                                   pathPattern:@"/auth/sign_in"
                                                                                       keyPath:nil
                                                                                   statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[manager addResponseDescriptor:responseDescriptor];

NSDictionary *parameters = @{
                             @"email": user.email,
                             @"password": user.password
                             };
[manager postObject:user path:@"/auth/sign_in" parameters:parameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSDictionary *headerFields = operation.HTTPRequestOperation.response.allHeaderFields;
    [self updateUserInfoForResponse:[mappingResult firstObject] headerFields:headerFields];
    if (successBlock) {

        successBlock([mappingResult firstObject]);
    }
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    if (failureBlock) {
        failureBlock(error.userInfo[RKObjectMapperErrorObjectsKey][0], error);
    }
}];

.m of JSONResponse class:

+ (RKObjectMapping *)mappingObject {
    RKObjectMapping *jsonResponseMapping = [RKObjectMapping mappingForClass:[JSONResponse class]];
    [jsonResponseMapping addAttributeMappingsFromDictionary:@{
                                                  @"status": @"status",
                                                  @"errors": @"errors",
                                                  }];

    RKRelationshipMapping *userRelationShip = [RKRelationshipMapping relationshipMappingFromKeyPath:@"data" toKeyPath:@"data" withMapping:[User mappingObject]];
    [jsonResponseMapping addPropertyMapping:userRelationShip];

    return jsonResponseMapping;
}

.h Of JSONResponse class :

#import <RestKit/Restkit.h>
#import <Foundation/Foundation.h>
#import "User.h"

@interface JSONResponse : NSObject

@property (nonatomic, copy) NSString *status;
@property (nonatomic) User *data;
@property (nonatomic, copy) NSDictionary *errors;

/**
 @function mappingObject

 @return RKObjectMapping mapping for the json response
 */
+ (RKObjectMapping *)mappingObject;

@end

.m of User class

#import "User.h"

@implementation User

+ (RKObjectMapping *)mappingObject {
    RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[User class]];
    [userMapping addAttributeMappingsFromDictionary:@{
                                                  @"email": @"email",
                                                  @"password": @"password",
                                                  @"gender": @"gender",
                                                  @"birthday": @"dateOfBirth",
                                                  @"city": @"city",
                                                  @"country": @"country"
                                                  }];
    return userMapping;
}

@end

.h of User class

@class RKObjectMapping;

@interface User : NSObject

@property (nonatomic, copy) NSString *email;
@property (nonatomic, copy) NSString *password;
@property (nonatomic, copy) NSString *gender;
@property (nonatomic, copy) NSString *dateOfBirth;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *country;

/**
    @function mappingObject

    @return RKObjectMapping mapping object for user
 */
+ (RKObjectMapping *)mappingObject;

@end

Is there anything wrong in my models objects ? The relationship is set properly right ? I do have the data property in the JSONResponse, and the JSON contains correctly the keyPath data. So I'm pretty confused why I have results when I remove my relationship, and why the mapping result is empty when I have the relationship. It's even doesn't go in the failureCallback, the operation is successful, the result is empty.

Any ideas ??

EDIT : Here's the logs :

2015-09-15 07:27:50.649 testRestkit[53698:3448725] D restkit.object_mapping:RKPropertyInspector.m:154 Cached property inspection for Class 'JSONResponse': {
    data = "<RKPropertyInspectorPropertyInfo: 0x7facf2c5fa00>";
    status = "<RKPropertyInspectorPropertyInfo: 0x7facf2c5f7d0>";
}
2015-09-15 07:27:50.650 testRestkit[53698:3448725] D restkit.object_mapping:RKPropertyInspector.m:154 Cached property inspection for Class 'User': {
    email = "<RKPropertyInspectorPropertyInfo: 0x7facf2c60680>";
}
2015-09-15 07:27:50.820 testRestkit[53698:3448725] I restkit.network:RKObjectRequestOperation.m:150 POST 'http://sandrotchikovani.com/test.php'
2015-09-15 07:27:51.236 testRestkit[53698:3448938] D restkit.object_mapping:RKMapperOperation.m:407 Executing mapping operation for representation: {
    data =     {
        email = "lol@gmail.com";
    };
    status = success;
}
 and targetObject: <User: 0x7facf2c05820>
2015-09-15 07:27:51.237 testRestkit[53698:3448938] T restkit.object_mapping:RKMapperOperation.m:350 Examining keyPath '<null>' for mappable content...
2015-09-15 07:27:51.237 testRestkit[53698:3448938] D restkit.object_mapping:RKMapperOperation.m:330 Found mappable data at keyPath '<null>': {
    data =     {
        email = "lol@gmail.com";
    };
    status = success;
}
2015-09-15 07:27:51.237 testRestkit[53698:3448938] D restkit.object_mapping:RKMapperOperation.m:433 Finished performing object mapping. Results: {
}

Isn't weird that in the log, it says and targetObject: <User: 0x7facf2c05820> ? When I remove the relationship, there is a mapping result and the targetObject, displays "null".


Solution

  • You are calling postObject:..., and when you do that RestKit will map back to the original object. In this case that's a user and that's why you're seeing the log and targetObject: <User: 0x7facf2c05820>.

    The easiest thing for you to do is to setup your JSONResponse so that you post it and receive the response into it. It already has the required user so a simple change to the request descriptor to pull out the user fields should be enough.

    Alternatively there are a bunch of other questions about mapping to a different object after posting.