为一个对象保存多个核心数据关系 swift
Saving Multiple Core Data relationships for one object swift
我正在尝试向一个对象添加多个核心数据关系。这是我的 CoreData table:
我正在使用此代码将对象添加到数据库:
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
Ik 得到一个错误,类型 serie 不包含 mutableOrderdSetValueForKey。
邮票对象需要同时与国家和意甲有关系。我正在使用以下代码创建与国家/地区对象的关系:currentCountry.mutableOrderedSetValueForKey("serie").addObjectsFromArray(arrayOfStamps)//Add Stamp to the database
所以我需要给新添加的戳记添加两个关系。如何正确有效地执行此操作?
我改变了它,所以我必须设置一对一的关系。更简单
我正在尝试向一个对象添加多个核心数据关系。这是我的 CoreData table:
我正在使用此代码将对象添加到数据库:
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
Ik 得到一个错误,类型 serie 不包含 mutableOrderdSetValueForKey。
邮票对象需要同时与国家和意甲有关系。我正在使用以下代码创建与国家/地区对象的关系:currentCountry.mutableOrderedSetValueForKey("serie").addObjectsFromArray(arrayOfStamps)//Add Stamp to the database
所以我需要给新添加的戳记添加两个关系。如何正确有效地执行此操作?
我改变了它,所以我必须设置一对一的关系。更简单