保存,删除和从一对多关系核心数据中获取数据

agf*_*105 6 core-data one-to-many relationship swift

我创建了一个这样的数据模型:

我有一个获取请求的代码:

func roundFetchRequest() -> NSFetchRequest {
    let fetchRequest = NSFetchRequest(entityName: "Customer")
    print("Check here: \(myRoundIndexPath)")
    //let predicate : NSPredicate = NSPredicate(format: "custRoundRel = %@", frc2.objectAtIndexPath(myRoundIndexPath!) as! RoundName) //ASSUME THIS IS CORRECT
    let sortDescriptor = NSSortDescriptor(key: "c2fna", ascending: true)
    //fetchRequest.predicate = predicate
    fetchRequest.sortDescriptors = [sortDescriptor]
    return fetchRequest
}
Run Code Online (Sandbox Code Playgroud)

我注释掉的代码没有出错,但我似乎无法将客户保存到RoundName实例.当我使用其属性保存客户时,我使用了以下代码:

func newCust() {
    let cont = self.context
    let newCustomer = NSEntityDescription.entityForName("Customer", inManagedObjectContext: cont)
    let aCust = Customer(entity: newCustomer!, insertIntoManagedObjectContext: cont)

    aCust.c2fna = firstName.text
    aCust.c3lna = lastName.text
    aCust.c4tel = tel.text
    aCust.c5mob = mob.text
    aCust.c6ema = email.text
    aCust.c7hsn = houseNo.text
    aCust.c8fir = street.text
    aCust.c9sec = secondLine.text
    aCust.c10ar = area.text
    aCust.c11pc = postcode.text
    aCust.c12cos = cost.text
    aCust.c13fq = frequencyNumber.text
    aCust.c14fqt = frequencyType.text
    let DF = NSDateFormatter()
    aCust.c15das = DF.dateFromString(startDate.text!)
    //Do Pics in a minute & next date in a minute
    aCust.c17notes = notes.text

    //print("Desc = \(picRound.image?.description)")

    do {
        try context.save()
        print("Save Successful")
    } catch {
        print("Save Unsuccessful")
    }
}
Run Code Online (Sandbox Code Playgroud)

将此客户与正确回合链接的代码是什么?

谢谢,我对核心数据非常新,真的很感激任何帮助.

Wai*_*ain 1

是的,您在获取请求上使用谓词,其格式如下

NSPredicate(format:"custRoundRel = %@", xxxx) 
Run Code Online (Sandbox Code Playgroud)

xxxx实例在哪里Round

您还可以roundCustRel根据您想要对实例执行的操作Customer以及实例的数量来使用该关系。