我正在尝试向一个对象添加多个核心数据关系。这是我的 CoreData 表:
我正在使用这段代码将对象添加到数据库:
for data in rawData{
if let itemID = Int(data.1.array![0].string!){
if let seriesID = Int(data.1.array![1].string!){
if let frontPictureId = Int(data.1.array![3].string!){
if let backPictureId = Int(data.1.array![4].string!){
if let itemDescription = data.1.array![5].string{
if let catatalogCodes = data.1.array![6].string{
if let itemName = data.1.array![7].string{
//Retrieve Serie for current stamp
var currentSerie: Serie!
privateMOC.performBlockAndWait{
let serieFetch = NSFetchRequest(entityName: "Serie")
serieFetch.predicate = NSPredicate(format: "seriesID == %d", seriesID)
do {
let results = try privateMOC.executeFetchRequest(serieFetch) as! [Serie]
if results.count > 0 {
currentSerie = results.first
}
} catch let error as NSError {
print("Error: \(error) " +
"description \(error.description)")
}
}
let stamp = NSEntityDescription.insertNewObjectForEntityForName("Stamp", inManagedObjectContext: privateMOC) as! Stamp
stamp.itemID = itemID
stamp.frontPictureID = frontPictureId
stamp.backPictureID = backPictureId
stamp.itemDescription = itemDescription
stamp.catalogCodes = catatalogCodes
stamp.itemName = itemName
//Make the series Relationship
//TODO: - Add RelationShip
arrayOfStamps.append(stamp)
}
}
}
}
}
}
}
}
currentCountry.mutableOrderedSetValueForKey("serie").addObjectsFromArray(arrayOfStamps)//Add Stamp to the database
privateMOC.performBlockAndWait(){
do {
try privateMOC.save()
} catch let error as NSError {
print("Could not save \(error), \(error.userInfo)")
}
}
但是在显示 TODO-:添加关系的行中需要添加什么?当我尝试使用它时:currentSerie.mutableOrderedSetValueForKey("stamps").addObject(stamp)//Add Stamp to serie
我得到一个错误,类型 serie,不包含 mutableOrderdSetValueForKey。
邮票对象需要同时与国家和意甲有关系。我使用以下代码创建与国家对象的关系:currentCountry.mutableOrderedSetValueForKey("serie").addObjectsFromArray(arrayOfStamps)//Add Stamp to the database
因此我需要为新添加的图章添加两个 关系。如何正确有效地执行此操作?
最佳答案
我改变了它,所以我必须设置一对一的关系。更简单
关于ios - 为一个对象快速保存多个核心数据关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37566514/