快速保存一个对象的多个核心数据关系

时间:2016-06-01 10:48:27

标签: ios swift core-data relationship

我正在尝试将多个Core Data关系添加到一个对象。这是我的CoreData表:Core Data 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我得到一个错误,类型系列,不包含mutableOrderdSetValueForKey。

邮票对象需要与国家和Serie同时建立关系。我使用以下代码创建与国家/地区对象的关系:currentCountry.mutableOrderedSetValueForKey("serie").addObjectsFromArray(arrayOfStamps)//Add Stamp to the database

所以我需要将两个关系添加到新添加的标记中。我该如何正确有效地做到这一点?

1 个答案:

答案 0 :(得分:0)

我改变了它,所以我必须设置为一个关系。方式更容易