使用核心数据iPhone中的代码在两个实体之间创建关系

时间:2012-07-16 18:37:16

标签: iphone ios core-data

在代码中创建ManageObjectModel时,我正在创建两个实体及其属性。但问题是如何在两个实体之间创建关系1到多个关系。我的代码如下。 我只想使用代码在两个员工和组织实体之间建立一对多的关系。

- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil) {
        return __managedObjectModel;
    }

    __managedObjectModel = [[NSManagedObjectModel alloc] init];

    NSEntityDescription *employeeEntity = [[NSEntityDescription alloc] init];
    [employeeEntity setName:@"Employee"];
    [employeeEntity setManagedObjectClassName:@"Employee"];

    NSEntityDescription *organizationEntity = [[NSEntityDescription alloc] init];
    [organizationEntity setName:@"Organization"];
    [organizationEntity setManagedObjectClassName:@"Organization"];
    [__managedObjectModel setEntities:[NSArray arrayWithObjects:employeeEntity, organizationEntity, nil]];

    NSAttributeDescription *nameAttribute = [[NSAttributeDescription alloc] init];    
    [nameAttribute setName:@"name"];
    [nameAttribute setAttributeType:NSDateAttributeType];
    [nameAttribute setOptional:NO];    

    NSAttributeDescription *idAttribute = [[NSAttributeDescription alloc] init];    
    [idAttribute setName:@"id"];
    [idAttribute setAttributeType:NSInteger32AttributeType];
    [idAttribute setOptional:NO];

    NSArray *properties = [NSArray arrayWithObjects: nameAttribute, idAttribute, nil];
    [employeeEntity setProperties:properties];

    NSAttributeDescription *organizationNameAttribute = [[NSAttributeDescription alloc] init];
    [organizationNameAttribute setName:@"Name"];
    [organizationNameAttribute setAttributeType:NSStringAttributeType];
    [organizationNameAttribute setOptional:NO];        

    properties = [NSArray arrayWithObjects:organizationNameAttribute, nil];
    [organizationEntity setProperties:properties];

    return __managedObjectModel;
}

2 个答案:

答案 0 :(得分:1)

如果您真的想在代码中执行此操作,则必须创建NSRelationshipDescription并将其添加到源对象的属性中。

这是doc

创建对象并在分配之前调用setMaxCount:。如果您查看isToMany方法的文档,您会看到它说:

YES if the receiver represents a to-many relationship (its maxCount is greater than 1) otherwise NO.

答案 1 :(得分:0)

以下是我们如何从代码(Swift 4)配置NSManagedObjectModel

public class IssueList: NSManagedObject {

   @NSManaged public var title: String
   @NSManaged public var issues: Set<Issue>
}

public class Issue: NSManagedObject {

   @NSManaged public var title: String
   @NSManaged public var issueList: IssueList
}

class PCDBStack: DBStack {

   override func setupModel() -> NSManagedObjectModel {

      let issueListEntity = makeIssueListEntity()
      let issueEntity = makeIssueEntity()

      // To-many relationship from "IssueList" to "Issue"
      let issueListToIssueRelation = NSRelationshipDescription()

      // To-one relationship from "Issue" to "IssueList"
      let issueToIssueListRelation = NSRelationshipDescription()

      issueListToIssueRelation.name = #keyPath(IssueList.issues)
      issueListToIssueRelation.minCount = 0
      issueListToIssueRelation.maxCount = 0 // max = 0 for to-many relationship
      issueListToIssueRelation.deleteRule = .cascadeDeleteRule
      issueListToIssueRelation.destinationEntity = issueEntity
      issueListToIssueRelation.inverseRelationship = issueToIssueListRelation

      issueToIssueListRelation.name = #keyPath(Issue.issueList)
      issueToIssueListRelation.minCount = 0
      issueToIssueListRelation.maxCount = 1 // max = 1 for to-one relationship
      issueToIssueListRelation.deleteRule = .nullifyDeleteRule
      issueToIssueListRelation.destinationEntity = issueListEntity
      issueToIssueListRelation.inverseRelationship = issueListToIssueRelation

      issueListEntity.properties.append(issueListToIssueRelation)
      issueEntity.properties.append(issueToIssueListRelation)

      let model = NSManagedObjectModel()
      model.entities = [issueEntity, issueListEntity]
      return model
   }

   private func makeIssueListEntity() -> NSEntityDescription {

      let attributeTitle = NSAttributeDescription()
      attributeTitle.name = #keyPath(IssueList.title)
      attributeTitle.attributeType = .stringAttributeType
      attributeTitle.isOptional = false

      let entityDescription = NSEntityDescription()
      entityDescription.name = IssueList.entityName
      entityDescription.managedObjectClassName = IssueList.entityClassName
      entityDescription.properties = [attributeTitle]

      return entityDescription
   }

   private func makeIssueEntity() -> NSEntityDescription {

      let attributeTitle = NSAttributeDescription()
      attributeTitle.name = #keyPath(Issue.title)
      attributeTitle.attributeType = .stringAttributeType
      attributeTitle.isOptional = false

      let entityDescription = NSEntityDescription()
      entityDescription.name = Issue.entityName
      entityDescription.managedObjectClassName = Issue.entityClassName
      entityDescription.properties = [attributeTitle]

      return entityDescription
   }
}

有用的NSManagedObject扩展程序:

extension NSManagedObject {

    public static var entityName: String {
        let className = NSStringFromClass(self) // As alternative can be used `self.description()` or `String(describing: self)`
        let entityName = className.components(separatedBy: ".").last!
        return entityName
    }

    public static var entityClassName: String {
        let className = NSStringFromClass(self)
        return className
    }
}