1
我从我的服务器端点得到以下JSON:RestKit - 无映射在嵌套的对象关系
{
(...),
"comments":[
{
"user":{
"account":"free",
"name":"BrucWayne",
"counter":{
"pictures":10,
"following":0,
"followers":0,
"tags":0
},
"id":"QYgZb"
},
"text":"How are you?",
"date":"2014-04-28T16:22:47+0200"
},
{
"user":{
"account":"free",
"name":"DevAbdul",
"counter":{
"pictures":19,
"following":0,
"followers":0,
"tags":1
},
"id":"AbADE"
},
"text":"ALA_MA_KOTA2",
"date":"2014-04-28T16:25:10+0200"
}
]
,(...)
}
我想有保存到CoreData结构正确关系的评论对象perserved。我在* .xcdatamodel文件定义为NSManagedObject
实例Comment
和User
对象模型:
评论:
用户:
这里是我的映射评论对象代码:
-(void) mapComment
{
commentsMapping = [RKEntityMapping mappingForEntityForName:@"Comment" inManagedObjectStore:[self managedObjectStore]];
[commentsMapping setIdentificationAttributes:@[@"date",@"text"]];
[commentsMapping addAttributeMappingsFromDictionary:@{@"text" : @"text",
@"@metadata.routing.parameters.pictureId" : @"pictureId",
@"user.id" : @"userId",
@"user.name" :@"userName",
@"date" : @"date"}];
[commentsMapping addConnectionForRelationship:@"user" connectedBy:@"userId"];
[commentsMapping addConnectionForRelationship:@"picture" connectedBy:@"pictureId"];
RKResponseDescriptor *commentShutReponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:commentsMapping method:RKRequestMethodAny pathPattern:@"/v1/pictures/:pictureId/comments" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
[[self objectManager] addResponseDescriptor:commentReponseDescriptor];
RKObjectMapping *commentRequestMapping = [RKObjectMapping requestMapping];
[commentRequestMapping addAttributeMappingsFromDictionary:@{@"text" : @"text"}];
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:commentRequestMapping objectClass:[Comment class] rootKeyPath:nil method:RKRequestMethodAny];
[self.objectManager addRequestDescriptor:requestDescriptor];
//Routes
RKRoute *commentPictureRelationshipRoute = [RKRoute routeWithClass:[Comment class] pathPattern:@"/v1/pictures/:pictureId/comments" method:RKRequestMethodAny];
[[[[self objectManager] router] routeSet] addRoute:commentPictureRelationshipRoute];
}
我在这个配置中遇到的问题是映射无法按预期工作。我设法正确地映射了Picture
对象,但不幸的是,我无法获得RestKit来映射User
对象。在被RestKit解析后,所有的用户引用都等于nil
。我想配置关系,使用户对象在CoreData中被正确识别并映射为一个新的User对象,并在它自己和Comment实例之间建立正确的关系。 我也想提一下,这种关系是一对多的关系。
所以问题是用户在请求之前不存在,你想同时创建和链接它们? – Wain
@是的,确切地说。 – Paul