RestKit对象映射与外键的关系 [英] RestKit Object Mapping relationships with foreign keys

查看:93
本文介绍了RestKit对象映射与外键的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以RestKit连接关系而不存储外键作为属性,即直接从JSON中的关键字?



特别是,我有一个工作has_many客房关系。房间的JSON不包含作业,而是两者分别加载:

   -  job:{
id :1,
name:John
}

- room:{
id:4,
job_id:1,
name :spare bedroom
}

作业在房间之前加载。



我的CoreData模型,Job有

的属性

  @interface作业:NSManagedObject 
@property(nonatomic,retain)NSNumber * identifier;
@property(nonatomic,retain)NSString * name;
@property(nonatomic,retain)NSSet * rooms;
@end

@interface房间:NSManagedObject
@property(nonatomic,retain)NSNumber * identifier;
@property(nonatomic,retain)NSString * name;
@property(nonatomic,retain)Job * job;
@end



目前我添加了一个 @property strong)NSNumber * jobID; 到Room,其中 @synthesize

  JobMapping:
mapping = [RKManagedObjectMapping mappingForClass:[Job class]];
[mapping setPrimaryKeyAttribute:@identifier];

[mapping mapAttributes:@name,nil];
[mapping mapKeyPath:@idtoAttribute:@identifier];

[mapping mapRelationship:@roomswithMapping:[Room objectMapping]];



RoomMapping
mapping = [RKManagedObjectMapping mappingForClass:[Room class]];
[mapping setPrimaryKeyAttribute:@identifier];

[mapping mapAttributes:@name,nil];
[mapping mapKeyPath:@idtoAttribute:@identifier];
[mapping mapKeyPath:@job_idtoAttribute:@jobID];

[mapping mapRelationship:@jobwithMapping:[Job objectMapping]];

[mapping connectRelationship:@jobwithObjectForPrimaryKeyAttribute:@jobID];

我想知道如果没有额外的jobID属性, / strong>我不想有一个jobID属性在CoreData xcdatamodeld - 它是多余的,因为关系涵盖了。



此外,如果我重建NSManagedObjects,需要重新添加jobID属性,这是冗长乏味的。我不能告诉restkit通过JSON中的job_id keypath将房间连接到相应的作业?



如果我删除该属性, mapKeyPath:@job_id行,并将最后一行更改为 [mapping connectRelationship:@jobwithObjectForPrimaryKeyAttribute:@job_id]; 我得到

 实体Room不是键值编码符合键job_id。 


解决方案

我会让JobId在核心数据中是一个瞬时值,



该集合会将关系设置为self.job = methodToReturnObjectMatchingJobId (这将被rest kit)



get会返回self.job.identifier



如果你不使用mogenerator



下面是我如何做的示例代码:

   - (void)setClaimId:(NSNumber *)claimId {
Claim * propertyClaim = [claim listItemFromId:[claimId intValue] withContext:self.managedObjectContext];

self.claim = propertyClaim;
}
- (NSNumber *)claimId {

return self.claim.referenceId;
}

其中listItemFromId是一个基于id返回对象的简单查询。 / p>

Can RestKit connect a relationship without storing the foreign key as an attribute, i.e., directly from the keypath in the JSON?

In particular, I've got a Job has_many Rooms relationship. The room's JSON doesn't contain the job, rather, both are loaded separately:

- job: {
    id: 1,
    name: "John"
}

- room: {
    id: 4,
    job_id: 1,
    name: "spare bedroom"
}

The Job is loaded before the Room.

My CoreData models, Job has properties for

@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

Currently I add a @property (nonatomic, strong) NSNumber *jobID; to Room, which I @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"];

I was wondering if there's a way I can do this without the extra jobID property? I don't want to have a jobID attribute in the CoreData xcdatamodeld - it's redundant, as the relationship covers that.

Also if I rebuild the NSManagedObjects, I need to re-add the jobID property, which is tedious. Can't I tell restkit to connect the Room to its corresponding Job via the job_id keypath in the JSON?

If I remove the property, the mapKeyPath:@"job_id" line, and change the last line to [mapping connectRelationship:@"job" withObjectForPrimaryKeyAttribute:@"job_id"]; I get

the entity Room is not key value coding-compliant for the key "job_id".

解决方案

I would make JobId a transient value in core data, and write a custom set and get for it.

The set would set the relationship to self.job=methodToReturnObjectMatchingJobId (this would be used by rest kit)

The get would return self.job.identifier

If you are not using mogenerator, I would suggest you have a look at it for all your core data needs.

Below is sample code of how i did it:

-(void) setClaimId:(NSNumber *)claimId{
     Claim *propertyClaim=[Claim listItemFromId:[claimId intValue] withContext:self.managedObjectContext]; 

    self.claim=propertyClaim; 
}
-(NSNumber*) claimId{

  return self.claim.referenceId;
}

where listItemFromId is a simple query that returns the object based on the id.

这篇关于RestKit对象映射与外键的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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