Restkit - 关系映射不起作用
Restkit - Relationship mapping not working
我正在体验 Restkit,使用版本 0.25.0
。我完全遵循了文档中关于关系映射的内容,但由于某些原因,我得到了一个空的映射结果!
但是当我删除关系对象(数据)时,我有一个映射结果!!!但是当然,映射结果不包含我需要的数据,即我添加为关系的数据。
我按照他们在 link :
中的示例
https://github.com/RestKit/RestKit/wiki/Object-mapping#relationships
当我用调试器打印 JSON 时,输出如下:
{
"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"
}
}
这是我发出请求的代码:
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 JSON响应 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 JSON 响应 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 用户 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 用户 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
我的模型对象有什么问题吗?关系设置得当吗?我在 JSON 响应中确实有数据 属性,并且 JSON 正确包含 keyPath data
。
所以我很困惑,为什么当我删除我的关系时我有结果,为什么当我有关系时映射结果是空的。连failureCallback都不进去,运行成功,结果为空。
有什么想法吗??
编辑:
这是日志:
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: {
}
日志中显示 and targetObject: <User: 0x7facf2c05820>
是不是很奇怪?当我删除关系时,有一个映射结果和 targetObject,显示 "null".
您正在调用 postObject:...
,当您这样做时,RestKit 将映射回原始对象。在这种情况下,这是一个用户,这就是您看到日志 and targetObject: <User: 0x7facf2c05820>
.
的原因
对您来说,最简单的事情就是设置您的 JSONResponse
,以便您 post 它并接收其中的响应。它已经具有所需的 user
,因此对请求描述符进行简单更改以提取用户字段就足够了。
另外,在 posting 之后还有很多关于映射到不同对象的其他问题。
我正在体验 Restkit,使用版本 0.25.0
。我完全遵循了文档中关于关系映射的内容,但由于某些原因,我得到了一个空的映射结果!
但是当我删除关系对象(数据)时,我有一个映射结果!!!但是当然,映射结果不包含我需要的数据,即我添加为关系的数据。
我按照他们在 link :
中的示例
https://github.com/RestKit/RestKit/wiki/Object-mapping#relationships
当我用调试器打印 JSON 时,输出如下:
{
"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"
}
}
这是我发出请求的代码:
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 JSON响应 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 JSON 响应 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 用户 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 用户 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
我的模型对象有什么问题吗?关系设置得当吗?我在 JSON 响应中确实有数据 属性,并且 JSON 正确包含 keyPath data
。
所以我很困惑,为什么当我删除我的关系时我有结果,为什么当我有关系时映射结果是空的。连failureCallback都不进去,运行成功,结果为空。
有什么想法吗??
编辑: 这是日志:
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: {
}
日志中显示 and targetObject: <User: 0x7facf2c05820>
是不是很奇怪?当我删除关系时,有一个映射结果和 targetObject,显示 "null".
您正在调用 postObject:...
,当您这样做时,RestKit 将映射回原始对象。在这种情况下,这是一个用户,这就是您看到日志 and targetObject: <User: 0x7facf2c05820>
.
对您来说,最简单的事情就是设置您的 JSONResponse
,以便您 post 它并接收其中的响应。它已经具有所需的 user
,因此对请求描述符进行简单更改以提取用户字段就足够了。
另外,在 posting 之后还有很多关于映射到不同对象的其他问题。