2015-01-31 103 views
0

我一直在努力处理这种关系连接。使用addConnectionForRelationship的RestKit 0.24映射

数据模型

enter image description here

我有以下JSON:

{ 
"response":{ 
"pageInfo":[ 
], 
"threads":{ 
"userId":"1", 
"threadId":"58", 
"status":"", 
"timestamp":"2015-02-03T13:24:29.000Z", 
"messages":[ 
{ 
"messageId":"23", 
"threadId":"58", 
"participantId":"106", 
"text":"test", 
"status":"", 
"timestamp":"2015-02-03 13:24:29" 
} 
], 
"participants":[ 
{ 
"participantId":"106", 
"threadId":"58", 
"userId":"1", 
"user":{ 
"id":"1", 
"username":"admin", 
"email":"[email protected]", 
"medias":[ 
{ 
"id":"181", 
"userId":"1", 
"serviceName":"surfin", 
"serviceURL":"http:\/\/media.go-surfin.com\/", 
"mimeType":"image\/jpeg", 
"size":"43505", 
"width":"480", 
"height":"320", 
"filename":"c5564a10-f950-4398-a655-c9b31a0e8500.jpeg", 
"url":"uploads\/avatar\/c5564a10-f950-4398-a655-c9b31a0e8500.jpeg", 
"name":"", 
"type":"AVATAR", 
"status":"APPROUVED", 
"comments":[ 
], 
"likeIds":[ 
], 
"timestamp":"2015-01-28T07:53:03.000Z" 
} 
], 
"stats":{ 
"userId":"1", 
"followers":2, 
"followings":3, 
"medias":3, 
"trips":0, 
"spots":506 
}, 
"profile":{ 
"country":"FR", 
"type":"SURFER", 
"pro":"0" 
}, 
"social":{ 
"facebook":"", 
"youtube":"", 
"twitter":"", 
"instagram":"" 
}, 
"created":"2014-09-10T11:05:08.000Z", 
"followingIds":[ 
"23", 
"19", 
"3" 
], 
"settings":[ 
{ 
"id":"17", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.newspot", 
"value":"0", 
"timestamp":"2014-11-12T16:52:20.000Z" 
}, 
{ 
"id":"18", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.newcontest", 
"value":"0", 
"timestamp":"2014-11-05T15:56:22.000Z" 
}, 
{ 
"id":"19", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.newmessage", 
"value":"0", 
"timestamp":"2014-11-04T09:14:29.000Z" 
}, 
{ 
"id":"20", 
"userId":"1", 
"type":"metrics", 
"key":"metrics.speed", 
"value":"0", 
"timestamp":"2015-01-25T17:57:31.000Z" 
}, 
{ 
"id":"22", 
"userId":"1", 
"type":"metrics", 
"key":"metrics.distance", 
"value":"0", 
"timestamp":"2014-11-30T11:17:31.000Z" 
}, 
{ 
"id":"23", 
"userId":"1", 
"type":"metrics", 
"key":"metrics.length", 
"value":"0", 
"timestamp":"2014-11-08T09:40:07.000Z" 
}, 
{ 
"id":"24", 
"userId":"1", 
"type":"metrics", 
"key":"metrics.forecast", 
"value":"0", 
"timestamp":"2014-11-30T11:17:31.000Z" 
}, 
{ 
"id":"25", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.commentspot", 
"value":"0", 
"timestamp":"2014-11-12T16:52:20.000Z" 
}, 
{ 
"id":"26", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.newplace", 
"value":"1", 
"timestamp":"2014-12-10T13:06:38.000Z" 
}, 
{ 
"id":"27", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.mediaplace", 
"value":"1", 
"timestamp":"2014-12-10T17:37:30.000Z" 
}, 
{ 
"id":"28", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.mediacontest", 
"value":"1", 
"timestamp":"2014-12-10T17:37:46.000Z" 
}, 
{ 
"id":"30", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.mediaspot", 
"value":"1", 
"timestamp":"2014-12-11T08:41:50.000Z" 
}, 
{ 
"id":"31", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.newtrip", 
"value":"1", 
"timestamp":"2015-01-16T14:13:37.000Z" 
}, 
{ 
"id":"32", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.mediatrip", 
"value":"1", 
"timestamp":"2014-12-11T08:53:17.000Z" 
}, 
{ 
"id":"33", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.jointrip", 
"value":"1", 
"timestamp":"2014-12-11T08:54:42.000Z" 
}, 
{ 
"id":"36", 
"userId":"1", 
"type":"notifications", 
"key":"notifications.commentplace", 
"value":"1", 
"timestamp":"2015-01-31T17:26:22.000Z" 
} 
], 
"favorites":{ 
"userId":"1", 
"placeIds":[ 
], 
"spotIds":[ 
"4698" 
], 
"contestIds":[ 
], 
"tripIds":[ 
] 
}, 
"likes":{ 
"userId":"1", 
"placeIds":[ 
], 
"spotIds":[ 
], 
"contestIds":[ 
], 
"tripIds":[ 
], 
"feedbackIds":[ 
], 
"mediaIds":[ 
], 
"statusIds":[ 
] 
} 
}, 
"status":"CREATOR", 
"timestamp":"2015-02-03 13:20:33" 
}, 
{ 
"participantId":"107", 
"threadId":"58", 
"userId":"2", 
"user":{ 
"id":"2", 
"username":"lgriffie", 
"email":"[email protected]", 
"medias":[ 
{ 
"id":"179", 
"userId":"2", 
"serviceName":"surfin", 
"serviceURL":"http:\/\/media.go-surfin.com\/", 
"mimeType":"image\/jpeg", 
"size":"18808", 
"width":"212", 
"height":"212", 
"filename":"a0de6758-97ac-4b84-9fae-e33b614640f0.jpeg", 
"url":"uploads\/avatar\/a0de6758-97ac-4b84-9fae-e33b614640f0.jpeg", 
"name":"", 
"type":"AVATAR", 
"status":"APPROUVED", 
"comments":[ 
], 
"likeIds":[ 
], 
"timestamp":"2014-09-10T19:27:05.000Z" 
} 
], 
"stats":{ 
"userId":"2", 
"followers":1, 
"followings":2, 
"medias":1, 
"trips":0, 
"spots":0 
}, 
"profile":{ 
"country":"US", 
"type":"FOLLOWER", 
"pro":"0" 
}, 
"social":{ 
"facebook":"", 
"youtube":"", 
"twitter":"", 
"instagram":"" 
}, 
"created":"2014-09-10T19:27:05.000Z", 
"followingIds":[ 
"3", 
"1" 
] 
}, 
"status":"NORMAL", 
"timestamp":"2015-02-03 13:20:33" 
} 
], 
"setting":{ 
"settingId":"7", 
"threadId":"58", 
"notification":"0", 
"block":"0", 
"timestamp":"2015-02-03 13:20:33" 
} 
}, 
"apiMethod":"threads:new" 
} 
} 

这里是为每个实体

THREAD

+ (RKEntityMapping *)mapping 
{ 
    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:[self entityName] 
                inManagedObjectStore:[RKManagedObjectStore defaultStore]]; 

[entityMapping addAttributeMappingsFromDictionary:@{ 
                @"threadId":   @"threadId", 
                @"userId":    @"userId", 
                @"status":    @"status", 
                @"timestamp":   @"timestamp" 
                }]; 
entityMapping.identificationAttributes = @[ @"threadId" ]; 

[entityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"messages" toKeyPath:@"messages" withMapping:[AOThreadMessage mapping]]]; 
[entityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"participants" toKeyPath:@"participants" withMapping:[AOThreadParticipant mapping]]]; 
[entityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"setting" toKeyPath:@"setting" withMapping:[AOThreadSetting mapping]]]; 

[entityMapping addConnectionForRelationship:@"user" connectedBy:@{ @"userId": @"userId" }]; 

[entityMapping setAssignsDefaultValueForMissingAttributes:YES]; 

return entityMapping; 
} 

+ (RKResponseDescriptor *)responseDescriptor 
{ 
    return [RKResponseDescriptor responseDescriptorWithMapping:[AOThread mapping] 
                 method:RKRequestMethodPOST | RKRequestMethodGET 
                pathPattern:nil 
                 keyPath:@"response.threads" 
                statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
} 
所定义的映射名

线程消息

+ (RKEntityMapping *)mapping 
{ 
    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:[self entityName] 
                inManagedObjectStore:[RKManagedObjectStore defaultStore]]; 

[entityMapping addAttributeMappingsFromDictionary:@{ 
                @"messageId":   @"messageId", 
                @"participantId":  @"participantId", 
                @"text":    @"text", 
                @"status":    @"status", 
                @"timestamp":   @"timestamp" 
                }]; 
entityMapping.identificationAttributes = @[ @"messageId" ]; 

[entityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"media" toKeyPath:@"media" withMapping:[AOMedia mapping]]]; 

[entityMapping setAssignsDefaultValueForMissingAttributes:YES]; 

return entityMapping; 
} 

+ (RKResponseDescriptor *)responseDescriptor 
{ 
    return [RKResponseDescriptor responseDescriptorWithMapping:[AOThreadMessage mapping] 
                 method:RKRequestMethodPOST | RKRequestMethodGET 
                pathPattern:nil 
                 keyPath:@"response.messages" 
                statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
} 

THREAD参与者

+ (RKEntityMapping *)mapping 
{ 
    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:[self entityName] 
                inManagedObjectStore:[RKManagedObjectStore defaultStore]]; 

[entityMapping addAttributeMappingsFromDictionary:@{ 
                @"participantId":  @"participantId", 
                @"userId":    @"userId", 
                @"status":    @"status", 
                @"timestamp":   @"timestamp" 
                }]; 
entityMapping.identificationAttributes = @[ @"participantId" ]; 

[entityMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"user" toKeyPath:@"user" withMapping:[AOUser mapping]]]; 

[entityMapping setAssignsDefaultValueForMissingAttributes:YES]; 

return entityMapping; 
} 

+ (RKResponseDescriptor *)responseDescriptor 
{ 
    return [RKResponseDescriptor responseDescriptorWithMapping:[AOThreadParticipant mapping] 
                 method:RKRequestMethodPOST | RKRequestMethodGET 
                pathPattern:nil 
                 keyPath:@"response.participants" 
                statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
} 

THREAD SETTING

+ (RKEntityMapping *)mapping 
{ 
    RKEntityMapping *entityMapping = [RKEntityMapping mappingForEntityForName:[self entityName] 
                inManagedObjectStore:[RKManagedObjectStore defaultStore]]; 

[entityMapping addAttributeMappingsFromDictionary:@{ 
                @"settingId":   @"settingId", 
                @"notification":  @"notification", 
                @"block":    @"block", 
                @"timestamp":   @"timestamp" 
                }]; 
entityMapping.identificationAttributes = @[ @"settingId" ]; 

[entityMapping setAssignsDefaultValueForMissingAttributes:YES]; 

return entityMapping; 
} 

+ (RKResponseDescriptor *)responseDescriptor 
{ 
    return [RKResponseDescriptor responseDescriptorWithMapping:[AOThreadSetting mapping] 
                 method:RKRequestMethodPOST | RKRequestMethodGET 
                pathPattern:nil 
                 keyPath:@"response.setting" 
                statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)]; 
} 

的AOThreadMessage螺纹关系是零。其他关系正确设置。

<AOThreadMessage: 0x7fa529a694d0> (entity: AOThreadMessage; id: 
0xd00000000004001c <x-coredata://23C60BAE-57BD-4904-BF7B-7D550DE13190/AOThreadMessage/p1> ; data: { 
    address = nil; 
    media = nil; 
    messageId = 20; 
    participantId = 106; 
    status = ""; 
    text = test; 
    thread = nil; 
    timestamp = "2015-02-03 13:20:33 +0000"; 
}) 

对于那些谁希望看到完整的RestKit数据映射记录,只需点击此处http://cloud.appsido.com/qN

回答

1

你的JSON包含的一切,所以你不需要外键映射。事实上,他们可能会导致你的问题,并导致只有最后一个项目实际上被添加到关系中(因为默认是替换而不是联合)。因此,请删除此处将使用的所有外键映射。

您不显示你的数据模型,但participantsmessagessetting都应该是双向的,其中的反比关系被称为thread,然后一切都应该工作正常(我看不到任何其他明显的问题)。

另外,您应该使用路径模式来防止显然不正确的响应描述符被始终测试。

+0

我已经编辑了数据模型和修改实体映射定义的问题。现在没有设置AOThreadMessage线程关系。除此之外,关系正确设置。无法弄清楚问题所在。关于使用路径模式确实是件好事。我会执行这个。让我知道基于模型,如果关系正确完成(我认为是这样,但谁知道:)) – Appsido 2015-02-03 13:30:44

+0

模型看起来不错。原始日志显示正在连接的消息。如果它们没有被使用,你需要摆脱所有其他的响应描述符 - 你只需要一个响应描述符来映射线程及其所有嵌套的内容。 – Wain 2015-02-03 14:51:53

+0

事实上,在我根据您的评论修改映射之前,原始日志显示了问题。我已经编辑了所有的儿子/映射定义。除非消息>线程关系没有完成,否则任何事情都可以。更具体地说,当我第一次从API获取线程的所有链接。但是,如果在给定的线程中发布消息(请参阅编辑的json),则消息不会链接到线程。关于响应描述符,由于消息描述符位于@“response.messages”而不是@“response.threads.messages”上,因此它们不会被考虑在内。 – Appsido 2015-02-03 16:19:15