在NSManagedObjectModel中添加关系以编程方式创建NSEntityDescription

时间:2012-12-06 12:02:56

标签: objective-c cocoa core-data nsmanagedobject nsentitydescription

当你编写一个使用CoreData的静态库时,包含一个普通的.xdatamodeld文件会进入项目,因为你不能只将它的编译版本(.momd)链接到你的二进制文件中,所以最好创建整个{ {1}}代码如下:

NSManagedObjectModel

一切都很完美......

但是!等等,如果我的NSAttributeDescription *dateAttribute = NSAttributeDescription.new; dateAttribute.name = @"timestamp"; dateAttribute.attributeType = NSDoubleAttributeType; dateAttribute.optional = NO; dateAttribute.indexed = YES; NSAttributeDescription *payloadAttribute = NSAttributeDescription.new; payloadAttribute.name = @"payload"; payloadAttribute.attributeType = NSBinaryDataAttributeType; payloadAttribute.optional = NO; payloadAttribute.indexed = NO; NSEntityDescription *entry = NSEntityDescription.new; entry.name = entry.managedObjectClassName = NSStringFromClass(MyCustomEntry.class); entry.properties = @[dateAttribute, payloadAttribute]; NSManagedObjectModel *mom = NSManagedObjectModel.new; mom.entities = @[entry]; 中有多个实体并且它们是相关的(多对齐,反向等等),那么在世界上我将如何在代码中连接它们,如上例所示,那个很好的Xcode编辑器,你可以通过几次鼠标点击建立关系吗?

示例

想象一下,我们有一个MyCustomElement类,它与上面代码中的MyCustomEntry几乎相同。现在,这是他们的界面,如果我使用Xcode生成实体,它们将如何出现:

NSManagedObjectModel

我需要为他们创建什么样的NSRelationshipDescription以及如何启动它?

1 个答案:

答案 0 :(得分:18)

关系由NSRelationshipDescription个对象描述。以下代码为“MyCustomEntry”创建了两个实体描述,“MyCustomElement”创建了关系

  • entries(MyCustomElement - > MyCustomEntry,to-many),
  • element(MyCustomEntry - > MyCustomElement,to-one),与entries相反。

两个实体都只有一个字符串属性“identifier”(用于保存一些代码行)。

<强>目标C:

NSEntityDescription *entry = [[NSEntityDescription alloc] init];
[entry setName:@"MyCustomEntry"];
[entry setManagedObjectClassName:@"MyCustomEntry"];

NSAttributeDescription *entryIdAttribute = [[NSAttributeDescription alloc] init];
entryIdAttribute.name = @"identifier";
entryIdAttribute.attributeType = NSStringAttributeType;

NSEntityDescription *element = [[NSEntityDescription alloc] init];
[element setName:@"MyCustomElement"];
[element setManagedObjectClassName:@"MyCustomElement"];

NSAttributeDescription *elementIdAttribute = [[NSAttributeDescription alloc] init];
elementIdAttribute.name = @"identifier";
elementIdAttribute.attributeType = NSStringAttributeType;

// To-many relationship from "Element" to "Entry":
NSRelationshipDescription *entriesRelation = [[NSRelationshipDescription alloc] init];

// To-one relationship from "Entry" to "Element":
NSRelationshipDescription *elementRelation = [[NSRelationshipDescription alloc] init];

[entriesRelation setName:@"entries"];
[entriesRelation setDestinationEntity:entry];
[entriesRelation setMinCount:0];
[entriesRelation setMaxCount:0]; // max = 0 for to-many relationship
[entriesRelation setDeleteRule:NSCascadeDeleteRule];
[entriesRelation setInverseRelationship:elementRelation];

[elementRelation setName:@"element"];
[elementRelation setDestinationEntity:element];
[elementRelation setMinCount:0];
[elementRelation setMaxCount:1]; // max = 1 for to-one relationship
[elementRelation setDeleteRule:NSNullifyDeleteRule];
[elementRelation setInverseRelationship:entriesRelation];

[entry setProperties:@[entryIdAttribute, elementRelation]];
[element setProperties:@[elementIdAttribute, entriesRelation]];

NSManagedObjectModel *mom = [[NSManagedObjectModel alloc] init];
[mom setEntities:@[entry, element]];

Swift (现已更新为Swift 3/4):

let entry = NSEntityDescription ()
entry.name = "MyCustomEntry"
entry.managedObjectClassName = "MyCustomEntry"

let entryIdAttribute = NSAttributeDescription()
entryIdAttribute.name = "identifier";
entryIdAttribute.attributeType = .stringAttributeType;

let element = NSEntityDescription()
element.name = "MyCustomElement"
element.managedObjectClassName = "MyCustomElement"

let elementIdAttribute =  NSAttributeDescription()
elementIdAttribute.name = "identifier"
elementIdAttribute.attributeType = .stringAttributeType

// To-many relationship from "Element" to "Entry":
let entriesRelation = NSRelationshipDescription()

// To-one relationship from "Entry" to "Element":
let elementRelation = NSRelationshipDescription ()

entriesRelation.name = "entries"
entriesRelation.destinationEntity = entry
entriesRelation.minCount = 0
entriesRelation.maxCount = 0  // max = 0 for to-many relationship
entriesRelation.deleteRule = .cascadeDeleteRule
entriesRelation.inverseRelationship = elementRelation

elementRelation.name = "element"
elementRelation.destinationEntity = element
elementRelation.minCount = 0
elementRelation.maxCount = 1 // max = 1 for to-one relationship
elementRelation.deleteRule = .nullifyDeleteRule
elementRelation.inverseRelationship = entriesRelation

entry.properties = [entryIdAttribute, elementRelation]
element.properties = [elementIdAttribute, entriesRelation]

let mom = NSManagedObjectModel()
mom.entities = [entry, element]

我已经测试了这段代码,它似乎有效,所以我希望它对你有用。