Group Group Group Group Group Group Group Group Group

Save multiple entries simultaneously to a single core data entity

My swift code below features a func that saves a string to core data. The problem it is only saving the UserDBObj.setValue closets to the end of the code not both. So it is only saving one a time. I want RDC and aa to be saved simultaneously. In the debug area only “aa” is seen when printed. Some stated to solve this just Loop over your values and create a new instance of Users for each value.

                      func openDatabse()
    {
      context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Users", in: context)
let newUser = NSManagedObject(entity: entity!, insertInto: context)
saveData(UserDBObj:newUser)
   }
     func saveData(UserDBObj:NSManagedObject)
    {
    UserDBObj.setValue("RDC", forKey: "username")
      UserDBObj.setValue("aa", forKey: "username")





print("Storing Data..")
do {
    try context.save()
} catch {
    print("Storing data Failed")
}

fetchData()
   }

@timswift Do you still have issues with this?

This topic was automatically closed after 166 days. New replies are no longer allowed.