从一对多关系核心数据中保存、删除和获取数据
saving, deleting and fetching data from a one to many relationship core data
我创建了一个这样的数据模型:
我有这个提取请求代码:
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
}
我注释掉的代码没有给出错误,但我似乎无法将客户保存到 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")
}
}
link 正确回合的这位客户的代码是什么?
谢谢,我对核心数据还很陌生,非常感谢任何帮助。
是的,您在提取请求中使用了谓词,格式类似于
NSPredicate(format:"custRoundRel = %@", xxxx)
其中 xxxx
是 Round
实例。
您也可以只使用 roundCustRel
关系,具体取决于您要对 Customer
个实例执行的操作以及实例的数量。
您创建 Customer
对象的方式与创建其他托管对象的方式相同。对于 link 具有正确 Round
对象的客户,只需设置 to-one 关系(Core Data 会自动为您设置反向关系)。
newCustomer.round = round
// or, with your arcane attribute names
newCustomer.custRoundRel = theDesiredRoundObject
要接触特定一轮的客户,您不需要提取请求或谓词。
round.customers
// or, with your arcane attribute names
round.roundCustRel
我创建了一个这样的数据模型:
我有这个提取请求代码:
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
}
我注释掉的代码没有给出错误,但我似乎无法将客户保存到 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")
}
}
link 正确回合的这位客户的代码是什么?
谢谢,我对核心数据还很陌生,非常感谢任何帮助。
是的,您在提取请求中使用了谓词,格式类似于
NSPredicate(format:"custRoundRel = %@", xxxx)
其中 xxxx
是 Round
实例。
您也可以只使用 roundCustRel
关系,具体取决于您要对 Customer
个实例执行的操作以及实例的数量。
您创建 Customer
对象的方式与创建其他托管对象的方式相同。对于 link 具有正确 Round
对象的客户,只需设置 to-one 关系(Core Data 会自动为您设置反向关系)。
newCustomer.round = round
// or, with your arcane attribute names
newCustomer.custRoundRel = theDesiredRoundObject
要接触特定一轮的客户,您不需要提取请求或谓词。
round.customers
// or, with your arcane attribute names
round.roundCustRel