无法在RestKit中映射NSManagedObject的对象数组 [英] Can't map an array of objects for NSManagedObject in RestKit

查看:165
本文介绍了无法在RestKit中映射NSManagedObject的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有用户类是 NSManagedObject 子类:

@interface User : NSManagedObject

  @property (nonatomic, retain) NSString * about;
  @property (nonatomic, retain) NSString * contactAddress;
  @property (nonatomic, retain) NSString * contactEmail;
  @property (nonatomic, retain) NSString * hobbies;
  @property (nonatomic, retain) NSArray* jobExperience;
  @property (nonatomic, retain) NSString * name;
  @property (nonatomic, retain) NSString * surname;
@end

正如你可以看到这个对象有一个NSArray对象的属性 JobExperience JobExperience 的定义如下:

As you can see this object has an NSArray property of objects JobExperience. JobExperience defined as follows:

@interface JobExperience : NSObject <NSCoding>

 @property (nonatomic,strong) NSString* position;
 @property (nonatomic,strong) NSString* organization;
 @property (nonatomic,strong) NSString* dateFrom;
 @property (nonatomic,strong) NSString* dateTo;
 @property (nonatomic,strong) NSString* website;
 @property (nonatomic,strong) NSString* location;
 @property (nonatomic,strong) NSString* jobDescription;
@end

在数据模型中,我有一个实体 其中 jobExperience 属性定义为 transformable 。这就是为什么我在其中实现NSCoding方法。
这是我的 User 的映射:

In the data model I have one entity User where jobExperience attribute defined as transformable. That's why I implement NSCoding methods in it. Here is my mapping for User:

 _sharedUserProfileMapping = [RKObjectMapping mappingForClass:[User class]];
    [_sharedUserProfileMapping addAttributeMappingsFromArray:@[
                                                               @"name", @"surname",
                                                               @"hobbies", @"interests",
                                                       @"contact_address", @"contact_email"
                                                              ]];

    RKObjectMapping* mappingForJobExperience = [RKObjectMapping mappingForClass:[JobExperience class]];
    [mappingForJobExperience addAttributeMappingsFromArray:@[
                                                             @"location",
                                                             @"website", @"organization" ,
                                                             @"position"  ,
                                                             @"date_from", @"date_to"
                                                             ]];

    [mappingForJobExperience addAttributeMappingsFromDictionary:@{@"description": @"jobDescription"}];

    [_sharedUserProfileMapping addRelationshipMappingWithSourceKeyPath:@"job_experience" mapping:mappingForJobExperience];

正如你所看到的,我添加了关系映射。当 User 只是一个 NSObject 子类所有映射完美!但是当我使它 NSManagedObject 子类时,我有一个异常:

As you can see I add relationship mapping. When User was just an NSObject subclass all mapped perfectly!!! But when I made it NSManagedObject subclass I got an exception:

映射一个多对一关系为 NSManagedObject 。关于通过mutable应用值[Set | Array] ValueForKey
restkit.object_mapping:RKMappingOperation.m:726映射 NSArray 关系对象从keyPath'job_experience'到'jobExperience '。值:(



*由于未捕获异常而终止应用程序'NSInvalidArgumentException',原因:'NSManagedObjects的实体'User'支持-mutableArrayValueForKey:属性'jobExperience''
*
第一次调用堆栈:

Mapping a to-many relationship for an NSManagedObject. About to apply value via mutable[Set|Array]ValueForKey restkit.object_mapping:RKMappingOperation.m:726 Mapped NSArray relationship object from keyPath 'job_experience' to 'jobExperience'. Value: ( "", "" ) * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSManagedObjects of entity 'User' do not support -mutableArrayValueForKey: for the property 'jobExperience'' * First throw call stack:

完美。只有这个数组的 JobExperience 对象的问题。

And just for clarifying other attributes map perfectly. The problem only with this array of JobExperience objects.

推荐答案

落入这里没有实现的区域。具有受管对象数组的外部对象(IIRC),但是从您的描述和外部受管对象和对象数组不工作。它不帮助你没有实际显示不同的映射,这样做和不工作,虽然...

You could be falling into an area that isn't implemented here. An outer object with an array of managed objects works (IIRC), but from your description and outer managed object and an array of objects doesn't work. It doesn't help that you aren't actually showing the different mappings that do and don't work though...

RestKit应该对目标数据类型决定做什么。也许这不适用于这种情况,因为它提供了一个关系规范,它可以假设目的地是一个核心数据关系。

RestKit should be introspecting on the destination data type to decide what to do. Perhaps that doesn't work for this situation as it is provided with a relationship spec, it may assume that the destination is a Core Data relationship.

无论如何,不寻常的有一个管理对象与自定义对象的数组。通常,您将使用2个受管对象和实体之间的关系。这更有意义,并在运行时提供更多的表达力和效率。

In any case, it's unusual to have a managed object with an array of custom objects. Usually you would use 2 managed objects and a relationship between the entities. This makes more sense and provides more expressive power and efficiency at runtime. This is what I'd recommend.

这可能是你需要设置 mappingForJobExperience.forceCollectionMapping = YES; 让它工作(猜测)。

It could be that you need to set mappingForJobExperience.forceCollectionMapping = YES; to make it work (a guess).

这篇关于无法在RestKit中映射NSManagedObject的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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