RestKit可以連接關係而不將外鍵存儲爲屬性,即直接來自JSON中的鍵路徑?RestKit對象與外鍵的映射關係
特別是,我有一個工作has_many房關係。房間的JSON不包含作業,而是兩者分別加載:
- job: {
id: 1,
name: "John"
}
- room: {
id: 4,
job_id: 1,
name: "spare bedroom"
}
作業在房間之前加載。
我CoreData模型,工作有
@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"];
我在想,如果有一種方法,我可以做到這一點沒有額外的作業ID屬性?我不想在CoreData xcdatamodeld中有一個jobID屬性 - 它是多餘的,因爲關係覆蓋了這個屬性。
此外,如果我重建NSManagedObjects,我需要重新添加jobID屬性,這是乏味的。我不能通過JSON中的job_id keypath來告訴restkit將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".
您是否已經發現如何正確連接核心數據中的關係,而沒有在真實關係旁邊具有冗餘ID屬性? – thejaz 2013-05-24 14:24:32