我正在尝试使用共享管理器使用RestKit在一个GET请求期间更新多个模型,例如:
[[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor];
其中responseDescriptor
具有映射,密钥路径和路径模式。
正在获取的JSON包含五个单独的模型,当从空应用程序开始时可以是相当大的,比如300个总对象(不同类型)。这是一个显示一般结构的编辑示例。事情看起来像这样,但应用程序中还有更多内容。
{
"rooms":[{"id":1,"name":"Hall A"},{"id":2,"name":"Hall B"}],
"sessions":[{"id":168,"title":"Pre-emptive value-added strategy","room_id":1}]
}
在一个映射中,我正在尝试使用RKConnectonDescription
,以便CoreData知道它们是基于外键相关的。例如:
NSRelationshipDescription *roomRelationship = [_sessionMapping.entity relationshipsByName][@"room"];
[_sessionMapping addConnection:[[RKConnectionDescription alloc] initWithRelationship:roomRelationship attributes:@{ @"roomRemoteId": @"remoteId" }]];
所有CoreData对象都使用remoteId属性作为通过json传入的id的标识属性。例如,Session映射如下所示:
_sessionMapping = [RKEntityMapping mappingForEntityForName:@"Session" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];
_sessionMapping.identificationAttributes = @[ @"remoteId" ];
[_sessionMapping addAttributeMappingsFromDictionary:@{
@"id" : @"remoteId",
@"title": @"title",
@"room_id": @"roomRemoteId"}];
并且房间映射看起来像这样:
_roomMapping = [RKEntityMapping mappingForEntityForName:@"Room" inManagedObjectStore:[RKObjectManager sharedManager].managedObjectStore];
_roomMapping.identificationAttributes = @[ @"remoteId" ];
[_roomMapping addAttributeMappingsFromDictionary:@{
@"id" : @"remoteId",
@"name": @"name"
}];
我发现当我从空数据库开始并获取所有对象时,只有部分会话有空间。会话模型确实有roomRemoteId
字段,房间有remoteId
。我发现其中一个会话没有房间,但确实有roomRemoteId
设置,我可以在数据库中查找remoteId
的房间。
我估计大约25%的会话看起来很好,但是其他75%的会话有roomRemoteId
但没有设置空间关系。
所以问题是,为什么有些会话正确映射而其他会话没有?
答案 0 :(得分:0)
感谢Wain的好建议!在将全局Restkit日志级别提升到跟踪之后,我注意到我在sessions
的Room上有一个反向关系 - 但是我已将它间隔开来而不是一对多。有很多日志条目看起来像这样:
D restkit.core_data:RKRelationshipConnectionOperation.m:198 Connected relationship 'room' to object '<Room: 0x96fad00> (entity: Room; id: 0x1082e150 <x-coredata:///Room/t3B6287B3-4478-4B64-9D83-9C85387C377D629> ; data: {
name = 2204;
remoteId = 24;
sessions = "0x1081b320 <x-coredata:///Session/t3B6287B3-4478-4B64-9D83-9C85387C377D233>";
})'
但其中没有任何一个会话。这是我的线索,我没有正确设置关系,这使得映射完全无法正常工作。一旦我确定一切都按预期工作。
以下是会话关系属性的屏幕截图,一旦我勾选了To-Many Relationship框,它就能正常工作: