将解析的数据提供给RKMapperOperation会抛出NSUnknownKeyException [英] Feeding parsed data to RKMapperOperation throws NSUnknownKeyException

查看:105
本文介绍了将解析的数据提供给RKMapperOperation会抛出NSUnknownKeyException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅我的其他问题了解背景信息: Will RestKit's动态映射解决了这个复杂的JSON映射?

See my other question for background: Will RestKit's dynamic mapping solve this complex JSON mapping?

由于服务器构造我需要转换为 NSManagedObject的json数据的方式 s,我正在传递已解析的json来执行直接对象映射,如下所示:

Due to the way the server structures the json data that I need to convert into NSManagedObjects, I'm passing the parsed json to do a direct object mapping, like so:

RKObjectMapping *mapping = [RKObjectMapping requestMapping];
[mapping addAttributeMappingsFromDictionary:@{@"id": @"id", @"name": @"name"}];
NSDictionary *mappingsDictionary = @{ [NSNull null]: mapping };
RKMapperOperation *mapper = [[RKMapperOperation alloc]
        initWithRepresentation:dataArray mappingsDictionary:mappingsDictionary];
NSError *mappingError = nil;
BOOL isMapped = [mapper execute:&mappingError];
if (isMapped && !mappingError) {
    for (id thing in mapper.mappingResult.array) {
        NSLog(@"Mapped the '%@' thing: %@", NSStringFromClass([thing class]), thing);
    }
}

NSMutableArray,dataArray,如下所示: [{id:1,name:AAA},{id:2,name:BBB},...]

The NSMutableArray, dataArray, looks like this: [ {id: 1, "name":"AAA"}, {id: 2, "name":"BBB"}, ...]

代码打印出一些字典,但我想要的是从我的数据模型类生成的Foo对象(.id& .name)。

And the code prints out a number of dictionaries, but what I want are Foo objects (.id & .name) generated from my data model class.

如果我使用:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"Foo" inManagedObjectStore:store];

商店运行良好,因为它正常正常RestKit请求,我收到错误:

where the store is working well as it succeeds on 'normal' RestKit requests, I get the error:

由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[valueForUndefinedKey:]:实体(null)不是密钥值编码兼容的密钥id 。'

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: the entity (null) is not key value coding-compliant for the key "id".'

我做错了什么?

更新:

根据Wain的建议,我补充说:

Following Wain's advice, I added:

RKManagedObjectMappingOperationDataSource *mappingDS = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:store.mainQueueManagedObjectContext cache:store.managedObjectCache];
mappingDS.operationQueue = [NSOperationQueue new];

在上面的代码之前,然后在创建映射器之后,在调用执行,我按照建议设置数据源:

before the above code, and then, after creating the mapper, and before calling execute, I set the data source, as suggested:

mapper.mappingOperationDataSource = mappingDS;

我得到了预期的 NSManagedObject

推荐答案

映射器操作本身并不知道如何创建要映射到的类的实例。实体映射是正确使用的,但您需要教映射器操作如何实例化 Foo 对象。为此,您需要创建一个 RKManagedObjectMappingOperationDataSource 并将其设置为 mappingOperationDataSource

The mapper operation does not itself know how to create the instances of the class that you are mapping to. The entity mapping is the correct one to use but you need to teach the mapper operation how to instantiate the Foo objects. To do that you need to create a RKManagedObjectMappingOperationDataSource and set it as the mappingOperationDataSource.

这篇关于将解析的数据提供给RKMapperOperation会抛出NSUnknownKeyException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆