如何在CoreData中保存多个条目?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在CoreData中保存多个条目?相关的知识,希望对你有一定的参考价值。
我有以下代码执行没有错误。问题是它只保存了最后一个条目(“Jack Daniels”,3)。如何更改它以便保存所有三个条目?
let employees = NSEntityDescription.insertNewObjectForEntityForName("Employees", inManagedObjectContext: managedObject)
employees.setValue("John Doe", forKey: "employeename")
employees.setValue(1, forKey: "id")
employees.setValue("Jane Doe", forKey: "employeename")
employees.setValue(2, forKey: "id")
employees.setValue("Jack Daniels", forKey: "employeename")
employees.setValue(3, forKey: "id")
do {
try managedObject.save()
} catch {
print("problem saving")
}
答案
let employees = NSEntityDescription.insertNewObjectForEntityForName("Employees", inManagedObjectContext: managedObject)
let employees1 = NSEntityDescription.insertNewObjectForEntityForName("Employees", inManagedObjectContext: managedObject)
let employees2 = NSEntityDescription.insertNewObjectForEntityForName("Employees", inManagedObjectContext: managedObject)
employees.setValue("John Doe", forKey: "employeename")
employees.setValue(1, forKey: "id")
employees1.setValue("Jane Doe", forKey: "employeename")
employees1.setValue(2, forKey: "id")
employees2.setValue("Jack Daniels", forKey: "employeename")
employees2.setValue(3, forKey: "id")
do {
try managedObject.save()
} catch {
print("problem saving")
}
另一答案
Swift 4.1。您不需要将保存代码放入for循环中。只需插入所有企业,然后一次性保存。
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let entity = NSEntityDescription.entity(forEntityName: "Employees", in: context)
let employeeNames = ["John Doe", "Jane Doe", "Jack Daniels"]
for (index, employee) in employeeNames.enumerated() {
let newUser = NSManagedObject(entity: entity!, insertInto: context)
newUser.setValue(employee, forKey: "employeename")
newUser.setValue(index, forKey: "id")
}
do {
try context.save()
} catch {
print("Failed saving")
}
// Alternative You can create your Entity class as below and insert entries.
import CoreData
class Employees: NSManagedObject {
@NSManaged var employeename: String
@NSManaged var id: String
func addNameAndId(name: String = "", id: String = "") throws {
if name.count > 0 {
self.employeename = name
self.id = id
} else {
throw NSError(domain: "", code: 100, userInfo: nil)
}
}
}
// Now time to insert data
let employeeNames = ["John Doe", "Jane Doe", "Jack Daniels"]
for name in employeeNames {
guard let emp = NSEntityDescription.insertNewObject(forEntityName: "Employees", into: context) as? Employees else {
print("Error: Failed to create a new Film object!")
return
}
do {
try emp.addNameAndId(name: name, id: "0")
} catch {
print("Error: \(error)\nThe quake object will be deleted.")
context.delete(emp)
}
}
// Save all the changes just made and reset the taskContext to free the cache.
if context.hasChanges {
do {
try context.save()
} catch {
print("Error: \(error)\nCould not save Core Data context.")
}
context.reset() // Reset the context to clean up the cache and low the memory footprint.
}
另一答案
更简洁的方法(并且可扩展)可以将您的名称数据加载到数组中,然后逐步完成。你真的不想对任意长度的数组硬编码variable1,variable2
let employeeNames = ["John Doe", "Jane Doe", "Jack Daniels"]
for (index, employee) in employeeNames.enumerate()
{
let employeeEntry = NSEntityDescription.insertNewObjectForEntityForName("Employees", inManagedObjectContext: managedObject)
employeeEntry.setValue("John Doe", forKey: "employeename")
employees.setValue(index, forKey: "id")
do {
try managedObject.save()
} catch {
print("problem saving")
}
}
以上是关于如何在CoreData中保存多个条目?的主要内容,如果未能解决你的问题,请参考以下文章