Core data 从现有数据库ID关系加载核心数据关系

Core data 从现有数据库ID关系加载核心数据关系,core-data,restkit,Core Data,Restkit,我有一个在表格中有大量数据的应用程序,我正在编写一个移动应用程序来获取一小部分数据。表中的数据使用ID表示关系,我试图将其加载到核心数据模型中并保留关系 但是似乎没有一种简单的方法来告诉核心数据,对于这种关系,我想要一个外键指向另一个表。取而代之的是,我有一个怪物,它是六个异步RestKit查询中的一个的简化版本,这些查询将返回来填充数据库: [manager postObject:nil path:@api/shiftservice/get参数:@{@workerIds:@[@1]}成功:^R

我有一个在表格中有大量数据的应用程序,我正在编写一个移动应用程序来获取一小部分数据。表中的数据使用ID表示关系,我试图将其加载到核心数据模型中并保留关系

但是似乎没有一种简单的方法来告诉核心数据,对于这种关系,我想要一个外键指向另一个表。取而代之的是,我有一个怪物,它是六个异步RestKit查询中的一个的简化版本,这些查询将返回来填充数据库:

[manager postObject:nil path:@api/shiftservice/get参数:@{@workerIds:@[@1]}成功:^RKObjectRequestOperation*操作,RKMappingResult*映射结果{ NSLog@Loaded移位:%d,[[mappingResult数组]计数]; NSManagedObjectContext*上下文=[AppDelegate managedObjectContext]; NSSet*positions=[context-fetchObjectsForEntityName:@NWPosition]; NSDictionary*positionDict=[NSMutableDictionary new]; 对于NWPosition*位置中的位置[positionDict setValue:position forKey:position.positionId.stringValue]; NSSet*workers=[context-fetchObjectsForEntityName:@NWWorker]; NSDictionary*workerDict=[NSMutableDictionary new]; 对于NWWorker*worker-in-workers[workerDict-setValue:worker-forKey:worker.workerId.stringValue]; NSSet*shift=[context-fetchObjectsForEntityName:@NWShift]; 对于NWShift*轮班制 { NWPosition*position=[positionDict valueForKey:shift.positionId.stringValue]; position.shift=[context-fetchObjectsForEntityName:@NWShift with-predicateformat:@positionId=%d,position.positionId]; shift.position=位置; NSSet*tradesAsGetShift=[context-fetchObjectsForEntityName:@NWTrade with-predicateformat:@getShiftId=%@,shift.shiftId]; 对于NWTrade*贸易中贸易getShift trade.getShift=shift; shift.tradesAsGetShift=tradesAsGetShift; NSSet*tradesgiveshift=[context-fetchObjectsForEntityName:@NWTrade with-predicateformat:@giveShiftId=%@,shift.shiftId]; 对于NWTrade*交易中的交易sagiveshift trade.giveShift=shift; shift.tradesAsGiveShift=tradesAsGiveShift; NWWorker*worker=[workerDict valueForKey:shift.workerId.stringValue]; worker.shift=[context-fetchObjectsForEntityName:@NWShift with-predicateformat:@workerId=%d,worker.workerId]; 轮班工人=工人; } }失败:^RKObjectRequestOperation*操作,N错误*错误{ NSLog@Failed要加载有错误的移位:%@[error localizedDescription]; }]; 我将Matt Gallagher的一行Fetch at的修改版本用于fetchObjectsForEntityName


不管怎样,这对我来说很可怕,好像我错过了一些明显的东西。是否有某种方法可以告诉核心数据有关数据库样式的外键?如果没有,是否有一种简单的方法来填充它们?因为对每一个实体进行这么多的抓取显然不是正确的方法。如果RestKit在这方面有所帮助,那就更好了。

没有,但应该没有必要设置相反的关系。

多亏了Wain和他富有洞察力的评论,我设法让RestKit以一种相当优雅的方式为我做到了这一点。下面是我如何设置外键关系的。我设置了每个关系的两端,以便任何一组数据都可以首先进入,并且关系仍然可以正确填充

首先,我设置了管理器,告诉它使用JSON序列化:

RKManagedObjectStore*store=[AppDelegate managedObjectStore]; RKObjectManager*manager=[[RKObjectManager alloc]initWithHTTPClient:oauthClient]; [RKObjectManager设置共享管理器:管理器]; [RKMIMETypeSerialization注册表类:[RKNSJSONSerialization类]forMIMEType:RKMIMETypeJSON]; [manager.HTTPClient registerHTTPOperationClass:[AFJSONRequestOperation类]]; [manager registerRequestOperationClass:[AFJSONRequestOperation类]]; [manager setAcceptHeaderWithMIMEType:RKMIMETypeJSON]; [manager setRequestSerializationMIMEType:RKMIMETypeJSON]; manager.managedObjectStore=商店; 接下来,我设置连接和响应描述符:

NSManagedObjectContext*上下文=[AppDelegate managedObjectContext]; NSDictionary*orgRels=[[NSEntityDescription entityForName:@NWOrg inManagedObjectContext:context]relationshipsByName]; NSDictionary*positionRels=[[NSEntityDescription entityForName:@NWPosition inManagedObjectContext:context]relationshipsByName]; // ... NSIndexSet*statusCodes=RKStatusCodeIndexSetForClassRKStatusCodeClassSuccessful; NSArray*orgConnections= @[[self connectionForRelation:@positions localKey:@orgId foreignKey:@orgId withRels:orgRels], [self-connectionForRelation:@workers localKey:@orgId foreignKey:@orgId withRels:orgRels]; [manager addResponseDescriptor:[RKResponseDescriptor ResponseDescriptor WithMapping: [self entityMappingForName:@NWOrg fromtico nary:@{@orgId:@orgId,@orgName:@orgName}键:@orgId和连接:orgConnections] 方法:RKRequestMethodAny路径模式:@api/orgservice/get-keyPath:nil-statusCodes:statusCodes]]; NSArray*位置连接= @[[self connectionForRelation:@org localKey:@orgId foreignKey:@orgId withRels:positionRels], [self-connectionForRelation:@Shift localKey:@positionId foreignKey:@positionId withRels:positionRels]; [manager addResponseDescriptor:[RKResponseDescriptor ResponseDescriptor WithMapping: [self-entityMappingForName:@NWPosition fromDictionary:@{@positionId:@positionId,@orgId:@orgId,@name:@positionName}带关键字:@positionId和Connections:positionConnections] 方法:RKRequestMethodAny路径模式:@api/positionservice/get-keyPath:nil状态码:状态码]]; // ... 然后,我可以做一个简单的请求,效果很好:

[manager postObject:nil path:@api/shiftservice/get参数:@{@workerIds:@[@1]} 成功:^RKObjectRequestOperation*操作,RKMappingResult*映射结果{ NSLog@Loaded移位:%d,[[mappingResult数组]计数]; }失败:^RKObjectRequestOperation*操作,N错误*错误{ NSLog@Failed要加载有错误的移位:%@[error localizedDescription]; }]; 注意:这使用了几个助手方法,我将在这里重复这些方法:

-RKConnectionDescription*connectionForRelation:NSString*关系 localKey:NSString*localKey foreignKey:NSString*foreignKey withRels:NSDictionary*rels { return[[RKConnectionDescription alloc]initWithRelationship:rels[relations] 属性:@{localKey:foreignKey}]; } -RKEntityMapping*entityMappingForName:NSString*name fromDictionary:NSDictionary*dict with key:NSString*key { RKEntityMapping*mapping=[RKEntityMappingForEntityForName:name inManagedObjectStore:[AppDelegate managedObjectStore]]; [映射addAttributeMappingsFromDictionary:dict]; mapping.assignsDefaultValueForMissingAttributes=是; mapping.AssignsInFormssingRelationships=是; mapping.identificationAttributes=@[key]; 回归映射; } -RKEntityMapping*entityMappingForName:NSString*name fromDictionary:NSDictionary*dict with key:NSString*key andConnections:NSArray*连接 { RKEntityMapping*mapping=[self-entityMappingForName:name fromDictionary:dict with key:key]; 对于RKConnectionDescription*连接中的连接 { [映射addConnection:connection]; } 回归映射; }
RestKit应该处理外键,核心数据不需要。显示您收到的JSON和您拥有的映射。您是否已将外键关系添加到映射中?