我需要使用 RestKit 将多个没有关系但位于同一个远程 API 上的对象加载到 CoreData 中(换句话说,下载远程对象以在 iOS 本地使用它们)
所以我已经设置了我的 Client 和 objectManager:
client = [RKClient clientWithBaseURL:[NSURL URLWithString:baseUrl]];
[client setUsername:@"someUsername"];
[client setPassword:@"somePassword"];
objectManager = [RKObjectManager managerWithBaseURLString:baseUrl];
[objectManager setClient:client];
然后我为一个 Object#1 设置了一些映射:
RKManagedObjectMapping* companyMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Company" inManagedObjectStore:objectManager.objectStore];
companyMapping.primaryKeyAttribute = @"backendID";
[companyMapping mapKeyPath:@"id" toAttribute:@"backendID"];
[companyMapping mapKeyPath:@"company_name" toAttribute:@"companyName"];
然后我为对象 #2 设置了一些映射:
RKManagedObjectMapping* eventMapping = [RKManagedObjectMapping mappingForEntityWithName:@"Event" inManagedObjectStore:objectManager.objectStore];
eventMapping.primaryKeyAttribute = @"backendID";
[eventMapping mapKeyPath:@"id" toAttribute:@"backendID"];
[eventMapping mapKeyPath:@"description" toAttribute:@"eventDescription"];
之后,我将 objectMapping 设置为 mappingProvider:
[objectManager.mappingProvider setObjectMapping:companyMapping forResourcePathPattern:remoteObjectPath];
[objectManager.mappingProvider setObjectMapping:eventMapping forResourcePathPattern:remoteObjectPath];
最后,我为这两个对象一个接一个地调用“loadObjects ...”:
[objectManager loadObjectsAtResourcePath:remoteCompaniesObjectPath delegate:self];
[objectManager loadObjectsAtResourcePath:remoteEventsObjectPath delegate:self];
在这之后会发生什么,来自公司和事件的数据以某种方式加载到事件表中,因此表数据完全不可用。如果我只运行loadObjectsAtResourcePath:remoteCompaniesObjectPath
,那么一切都按预期工作,远程公司对象下载到 CoreData 中它自己的表中。
虽然它为什么会发生是有道理的(数据对于公司开始加载并获得新的映射,所以它加载到错误的表中)问题是:如何调用loadObjectsAtResourcePath:remoteObjectPath
方法来加载多个不相关的对象,以便它们可以正确加载到各自的表中在核心数据???