解决核心数据关系引发SIGABRT错误

时间:2019-09-27 15:14:47

标签: swift core-data

我正在使用Swift Playground重新学习Swift的核心数据基础。

我正在长时间写出核心数据,以编写一个简单的游乐场应用程序,其中

  

一个Company有很多Employees

我经常遇到错误:

  

错误:执行被中断,原因:信号SIGABRT。

在挽救公司与一名员工之间的关系时,但我不确定为什么要提高这种关系。

我的代码如下:

// Swift playground code
import CoreData

class NotificationListener: NSObject {
    @objc func handleDidSaveNotification(_ notification:Notification) {
        print("did save notification received: \(notification)")
    }
}

let listener = NotificationListener()
NotificationCenter.default.addObserver(listener, selector: #selector(NotificationListener.handleDidSaveNotification(_:)), name: NSNotification.Name.NSManagedObjectContextDidSave, object: nil)


// Define managed object
let model = NSManagedObjectModel()

//: [Entities]

let companyEntity = NSEntityDescription()
companyEntity.name = "Company"

let employeeEntity = NSEntityDescription()
employeeEntity.name = "Employee"
employeeEntity.indexes = []

//: [Attributes]

let companyNameAttribute = NSAttributeDescription()
companyNameAttribute.name = "name"
companyNameAttribute.attributeType = NSAttributeType.stringAttributeType
companyNameAttribute.isOptional = false

let countryAttribute = NSAttributeDescription()
countryAttribute.name = "country"
countryAttribute.attributeType = NSAttributeType.stringAttributeType
countryAttribute.isOptional = false

let employeeNameAttribute = NSAttributeDescription()
employeeNameAttribute.name = "name"
employeeNameAttribute.attributeType = NSAttributeType.stringAttributeType
employeeNameAttribute.isOptional = false

let ageAttribute = NSAttributeDescription()
ageAttribute.name = "age"
ageAttribute.attributeType = NSAttributeType.integer16AttributeType
ageAttribute.isOptional = false

// Relationships

let companyRelationship = NSRelationshipDescription()
let employeeRelationship = NSRelationshipDescription()

companyRelationship.name = "company"
companyRelationship.destinationEntity = companyEntity
companyRelationship.minCount = 0
companyRelationship.maxCount = 0
companyRelationship.deleteRule = NSDeleteRule.cascadeDeleteRule
companyRelationship.inverseRelationship = employeeRelationship

employeeRelationship.name = "employees"
employeeRelationship.destinationEntity = employeeEntity
employeeRelationship.minCount = 0
employeeRelationship.maxCount = 1
employeeRelationship.deleteRule = NSDeleteRule.nullifyDeleteRule
employeeRelationship.inverseRelationship = companyRelationship

companyEntity.properties = [companyNameAttribute, countryAttribute, employeeRelationship]
employeeEntity.properties = [employeeNameAttribute, ageAttribute, companyRelationship]

model.entities = [companyEntity, employeeEntity]

// Create persistent store coordinator
let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel:model)

do {
    try persistentStoreCoordinator.addPersistentStore(ofType: NSInMemoryStoreType, configurationName: nil, at: nil, options: nil)
} catch {
    print("error creating persistentStoreCoordinator: \(error)")
}

let managedObjectContext = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = persistentStoreCoordinator

// Companies
let companyABC = NSEntityDescription.insertNewObject(forEntityName: "Company", into: managedObjectContext)
companyABC.setValue("ABC Ltd", forKeyPath: "name")
companyABC.setValue("United States", forKeyPath: "country")

let companyDelta = NSEntityDescription.insertNewObject(forEntityName: "Company", into: managedObjectContext)
companyDelta.setValue("Delta", forKeyPath: "name")
companyDelta.setValue("Canada", forKeyPath: "country")

let tom = NSEntityDescription.insertNewObject(forEntityName: "Employee", into: managedObjectContext)
tom.setValue("Tom", forKey: "name")
tom.setValue(22, forKey: "age")
tom.setValue(companyABC, forKey: "company") // <<-- Throws error

let sarah = NSEntityDescription.insertNewObject(forEntityName: "Employee", into: managedObjectContext)
sarah.setValue("Sarah", forKey: "name")
sarah.setValue(41, forKey: "age")
sarah.setValue(companyDelta, forKey: "company")   // <<-- Throws error



func save(context: NSManagedObjectContext) {

    // Save context
    do {
        try context.save()
    } catch {
        print("error saving context: \(error)")
    }

    let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Company")

    var results: [NSManagedObject] = []

    do {
        results = try managedObjectContext.fetch(fetchRequest)

        print ("\n#\(results.count) records found\n")

    } catch {
        print("error executing fetch request: \(error)")
    }

    print("results: \(results)")
}

save(context: managedObjectContext)

当它试图挽救一名员工时出现问题:

let tom = NSEntityDescription.insertNewObject(forEntityName: "Employee", into: managedObjectContext)
tom.setValue("Tom", forKey: "name")
tom.setValue(22, forKey: "age")
tom.setValue(companyABC, forKey: "company")

尝试将companyABC设置为tom对象的关系时,会引发错误。

目标是使Tom成为companyABC的雇员

我相信这种关系已经正确建立。

但是我不确定是什么导致了错误。

因此,我的查询是:如何解决此错误?

谢谢

1 个答案:

答案 0 :(得分:1)

...
tom.setValue(Set([companyABC]), forKey: "company")
...
sarah.setValue(Set([companyDelta]), forKey: "company")
...

因为在这种情况下,如果您要从CoreData图使用XCode生成类模型,则它会生成对象,其中属性company(NS)Set我认为应该将其写在CoreData文档中的某个位置,但是不幸的是set是一个太普通的词。编辑找到它。

来自doc

  

“目标”弹出菜单定义在代码中访问关系时返回的一个或多个对象。如果将关系定义为一对一,则返回单个对象(如果关系可以是可选的,则返回nil)。 如果该关系被定义为一对多,则返回一个集合(或者,如果该关系可以是可选的,则返回nil)。