Restkit - Relationship mapping not working

时间:2015-09-14 16:09:34

标签: ios objective-c restkit restkit-0.20

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".

1 个答案:

答案 0 :(得分:1)

您正在调用postObject:...,当您这样做时,RestKit将映射回原始对象。在这种情况下,这是一个用户,这就是你看到日志and targetObject: <User: 0x7facf2c05820>

的原因

您最简单的方法是设置JSONResponse,以便发布并收到回复。它已经具有所需的user,因此对请求描述符进行简单的更改以提取用户字段就足够了。

或者,在发布后,还有许多关于映射到不同对象的其他问题。

相关问题