简体   繁体   English

使用coredata iPhone中的代码在两个实体之间创建关系

[英]Create Relation between two entities using code in coredata iPhone

while creating ManageObjectModel in code I am creating two entities and their attribute. 在代码中创建ManageObjectModel时,我正在创建两个实体及其属性。 But problem is how i can create relation 1 to many relation between two entity. 但是问题是我如何在两个实体之间创建关系1到许多关系。 My code is below. 我的代码如下。 I just want to make 1 to many relation between two employee and organization entities using code. 我只想使用代码在两个员工和组织实体之间建立一对多关系。

- (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;
}

If you really want to do it in code, you have to create a NSRelationshipDescription and add it to the properties of the source object. 如果您确实想在代码中执行此操作,则必须创建一个NSRelationshipDescription并将其添加到源对象的属性中。

Here is the doc . 这是文档

Create the object and call setMaxCount: before assigning it. 创建对象并在分配对象之前调用setMaxCount: If you look at the documentation for the isToMany method, you will see that it says: 如果查看isToMany方法的文档,您会看到它说:

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

Here is how we configured NSManagedObjectModel from code (Swift 4). 这是我们通过代码(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
   }
}

Useful NSManagedObject extension: 有用的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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM