gpt4 book ai didi

ios - Restkit - 关系映射不起作用

转载 作者:行者123 更新时间:2023-11-29 01:39:31 25 4
gpt4 key购买 nike

我正在体验 Restkit,使用版本 0.25.0 。我完全遵循文档中关于关系映射的说明,但由于某些原因,我有一个空的映射结果!但是当我删除关系对象(数据)时,我得到了一个映射结果!但是,当然,映射结果不包含我需要的数据,即我作为关系添加的数据。我按照他们在此链接中提供的示例进行操作:
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 的 JSONResponse 类:

+ (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 的 JSONResponse 类:

#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

#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 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

我的模型对象有什么问题吗?关系设置得当吗?我确实在 JSONResponse 中有 data 属性,并且 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这样您就可以发布它并收到其中的回复。它已经具有所需的 user因此,对请求描述符进行简单更改以提取用户字段就足够了。

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

关于ios - Restkit - 关系映射不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32569230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com