3

我从大约 6 个月前提交的对 Restkit 的更改中看到——Restkit 支持将 JSON 结构与外键数组映射:</p>

supports structure such as:
{
  items: [{id: 1}, {id: 2}, {id: 3}],
  itemgroups: [{items: [1, 2]}, {items:[2, 3]}]
}

我的问题是,谁能告诉我如何将这个功能与核心数据一起使用?

我确实检查了 Restkit 源代码,发现以下代码演示了更改的用法。但它们似乎不适用于 CoreData。

RKManagedObjectMapping *humanMapping = [RKManagedObjectMapping mappingForClass:[RKHuman class] inManagedObjectStore:objectStore];

humanMapping.primaryKeyAttribute = @"railsID";
[humanMapping mapAttributes:@"name", @"favoriteCatID", @"catIDs", nil];
[humanMapping mapRelationship:@"cats" withMapping:catMapping];
[humanMapping connectRelationship:@"cats" withObjectForPrimaryKeyAttribute:@"catIDs"];

人类对象中的 catIDs 属性是一个 NSArray:

@interface RKHuman : NSManagedObject {
}

@property (nonatomic, retain) NSNumber *railsID;
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSSet *cats;
@property (nonatomic, retain) NSArray *catIDs;

测试用例是通过 NSArray 实现的。但是我们都知道Coredata不能生成NSArray类型的属性。所以这个演示代码在现实世界中没有意义,这只是为了测试目的。有谁知道我应该如何使用这个功能:通过主键的多对多关系?

4

0 回答 0