RestKit 是否可以在不将外键存储为属性的情况下(即直接从 JSON 中的键路径)连接关系?
特别是,我有一个 Job has_many Rooms 关系。房间的 JSON 不包含作业,而是分别加载:
- job: {
id: 1,
name: "John"
}
- room: {
id: 4,
job_id: 1,
name: "spare bedroom"
}
Job 在 Room 之前加载。
我的 CoreData 模型,Job 具有以下属性
@interface Job : NSManagedObject
@property (nonatomic, retain) NSNumber * identifier;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSSet *rooms;
@end
@interface Room : NSManagedObject
@property (nonatomic, retain) NSNumber * identifier;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Job *job;
@end
目前我添加了一个@property (nonatomic, strong) NSNumber *jobID;
到房间,我@synthesize
.
JobMapping:
mapping = [RKManagedObjectMapping mappingForClass:[Job class]];
[mapping setPrimaryKeyAttribute:@"identifier"];
[mapping mapAttributes:@"name", nil];
[mapping mapKeyPath:@"id" toAttribute:@"identifier"];
[mapping mapRelationship:@"rooms" withMapping:[Room objectMapping]];
RoomMapping
mapping = [RKManagedObjectMapping mappingForClass:[Room class]];
[mapping setPrimaryKeyAttribute:@"identifier"];
[mapping mapAttributes:@"name", nil];
[mapping mapKeyPath:@"id" toAttribute:@"identifier"];
[mapping mapKeyPath:@"job_id" toAttribute:@"jobID"];
[mapping mapRelationship:@"job" withMapping:[Job objectMapping]];
[mapping connectRelationship:@"job" withObjectForPrimaryKeyAttribute:@"jobID"];
我想知道是否有一种方法可以在没有额外的 jobID 属性的情况下做到这一点? 我不想在 CoreData xcdatamodeld 中有 jobID 属性——它是多余的,因为关系涵盖了这一点。
此外,如果我重建 NSManagedObjects,我需要重新添加 jobID 属性,这很乏味。我不能告诉 restkit 通过 JSON 中的 job_id 键路径将 Room 连接到其相应的 Job 吗?
如果我删除该属性,该mapKeyPath:@"job_id"
行并将最后一行更改为[mapping connectRelationship:@"job" withObjectForPrimaryKeyAttribute:@"job_id"];
我得到
the entity Room is not key value coding-compliant for the key "job_id".