0

在代码中创建 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;
}
4

2 回答 2

1

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

这是文档

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

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

于 2012-07-16T19:12:37.020 回答
0

这是我们NSManagedObjectModel从代码(Swift 4)进行配置的方式。

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
    }
}
于 2018-05-04T18:32:41.017 回答