ResKit iOS - 为同一个元素注册多个类
Posted
技术标签:
【中文标题】ResKit iOS - 为同一个元素注册多个类【英文标题】:ResKit iOS - Register multiple classes for same Element 【发布时间】:2011-06-13 16:56:24 【问题描述】:您好 *** 社区, 我正在使用 RKObjectManager 发出 ios RESTful 请求以将响应映射到本地对象。
我对 Restkit 的问题是在不同的请求中为同一个元素注册多个类。在这个请求中,我嵌套了具有相同元素名称“列表”的对象。
下面是部分代码:
// Get array of RKObjects1 - First request of the queue
RKObjectManager * objectManager = [RKObjectManager sharedManager];
[objectManager registerClass:[RKTObject1 class] forElementNamed:@"List"];
RKObjectLoader * loader = [objectManager objectLoaderWithResourcePath:@"objects1" delegate:self];
[loader setObjectClass:[RKTList class]];
// Get array of RKObjects2 - Second request of the queue
RKObjectManager * objectManager = [RKObjectManager sharedManager];
[objectManager registerClass:[RKObjects2 class] forElementNamed:@"List"];
RKObjectLoader * loader = [objectManager objectLoaderWithResourcePath:@"objects2" delegate:self];
[loader setObjectClass:[RKTList class]];
我想知道,是否有任何解决方案可以在不同请求中为同一元素注册多个类?
【问题讨论】:
【参考方案1】:好吧,为了解决我的问题,我更新了 restkit 并使用新的 Object Mapping 2.0 可以为相同的元素键注册多个类。
您只需为每个请求创建不同的RKObjectMapping对象,并在RKObjectLoader中对每个对象进行setObjectMapping即可。
例如:
RKObjectMappingProvider * mappingProvider = [RKObjectManager sharedManager].mappingProvider;
// Create 2 RKObjectMapping for each request
RKObjectMapping * object1Mapping = [RKObjectMapping mappingForClass:[RKTList class]];
[object1Mapping mapKeyPath:@"Id" toAttribute:@"idObject"];
[object1Mapping mapKeyPath:@"Name" toAttribute:@"name"];
[mappingProvider setMapping:object1Mapping forKeyPath:@"Objects1List"];
RKObjectMapping * object2Mapping = [RKObjectMapping mappingForClass:[RKTList class]];
[object2Mapping mapKeyPath:@"Id" toAttribute:@"idObject"];
[object2Mapping mapKeyPath:@"Name" toAttribute:@"name"];
[mappingProvider setMapping:object2Mapping forKeyPath:@"Objects2List"];
//Make the 2 request with explicit object mapping
RKObjectManager * objectManager = [RKObjectManager sharedManager];
RKObjectLoader * loaderFirstRequest = [objectManager objectLoaderWithResourcePath:@"objects1" delegate:self];
[loaderFirstRequest setObjectMapping:object1Mapping];
RKObjectLoader * loaderSecondRequest = [objectManager objectLoaderWithResourcePath:@"objects2" delegate:self];
[loaderSecondRequest setObjectMapping:object2Mapping];
【讨论】:
这是如何工作的?我以为您说每个列表都返回“列表”键路径,但我看到您使用两个单独的键路径 - Objects1List 和 Objects2List? [objectManager objectLoaderWithResourcePath: delegate:] 具有 void 返回类型。它如何返回一个 RKObjectLoader?以上是关于ResKit iOS - 为同一个元素注册多个类的主要内容,如果未能解决你的问题,请参考以下文章