Not able to save to Core Data. Why?

Hello:


I have the below code. I am trying to save values from the backgroundContext "Items" to the column "answer" (I come from an Oracle background) in Core Data. Line 21 shows that the variable "answerItem" has the correct data for each loop but when I check the Data Store the "answer" column is blank.


Any help will be appreciates to edit the code so that the data will be saved.


let backgroundContext = persistentContainer.newBackgroundContext ()

persistentContainer.viewContext.automaticallyMergesChangesFromParent = true

let entity = NSEntityDescription.entity(forEntityName: "TempSCQ", in: backgroundContext)!

var answerItem: String = ""
        for item in items {

        answerItem = ""

           let newEntity = NSManagedObject(entity: entity, insertInto: backgroundContext)

             if let record = item.value(forKeyPath: "answer") as? String {

                     newEntity.setValue(answerItem, forKey: "answer")

                     answerItem.append(record)


                print ("Answer Item is \(answerItem)")

                print ("PProcess E1")
           }

              do

                {

                    try backgroundContext.save()

                    //                                                                        print("saved")

                }

                catch

                {

                }

            }

        }

}

Replies

SOLVED


for item in items {
              answerItem = ""
                 let newEntity = NSManagedObject(entity: entity, insertInto: backgroundContext)
                   if let record = item.value(forKeyPath: "answer") as? String {
                           newEntity.setValue(record, forKeyPath: "answer")
                           items.append(newEntity)
                     
                      print ("Answer Item is \(record)")

                  }
                 
                    do
                      {
                          try backgroundContext.save()
                          //                                                                        print("saved")
                      }
                      catch
                      {
                      }

Was there an error to catch?