0

I'm trying to add multiple Core Data relationships to one object. This is my CoreData table:Core Data 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?

sloeberGJ
  • 355
  • 3
  • 13
  • Possible duplicate of [Saving CoreData to-many relationships in Swift](http://stackoverflow.com/questions/25127090/saving-coredata-to-many-relationships-in-swift) – sschale Jun 01 '16 at 19:02
  • No, because here I want to add _multiple_ relationships to an object, which doesn't work as I expect it to do. That's why I'm asking this question. I hope the clarification helped. – sloeberGJ Jun 02 '16 at 17:09

1 Answers1

-1

I changed it so that I had to set the to one relationship. Way easier

sloeberGJ
  • 355
  • 3
  • 13