I'm trying to add multiple Core Data relationships to one object. This is my CoreData table:
Im using this code to add objects to the database:
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)")
}
}
But what needs to come on the line where it says TODO-: Add Relationship? When I try to use this: currentSerie.mutableOrderedSetValueForKey("stamps").addObject(stamp)//Add Stamp to serie
Ik get an error that the type serie, doesn't contain a mutableOrderdSetValueForKey.
The stamp object needs to have a relationship with country and with Serie at the same time. Im creating the relationship with the country object using this code: currentCountry.mutableOrderedSetValueForKey("serie").addObjectsFromArray(arrayOfStamps)//Add Stamp to the database
So I need to add two relationships to the newly added stamp. How do I do this correctly and efficiently?