保存到多个实体-核心数据-Swift

时间:2018-09-03 20:34:20

标签: ios swift core-data

希望这是有道理的。

我正在尝试将数据从一个功能存储到2个不同的实体中,但是将数据存储到第二个实体中并没有太多运气。

我的2个实体称为-工作 ShootKitChecklist

我已经创建了一个核心数据管理器,我将其剥离下来只是向您展示我正在使用的部分:

struct CoreDataManager {

static let shared = CoreDataManager()

let persistentContainer: NSPersistentContainer = {
    // initialization of core data stack
    let container = NSPersistentContainer(name: "TBAShootCoreData")
    container.loadPersistentStores { (storeDescription, error) in
        if let error = error {
            fatalError("loading of store failed: \(error)")
        }
    }
    return container
}()

func createSKCItem(item: String, used: Bool, visible: Bool, job: Job) -> (ShootKitChecklist?, Error?) {
    let context = persistentContainer.viewContext
    // create Shoot kit item
    let SKC = NSEntityDescription.insertNewObject(forEntityName: "ShootKitChecklist", into: context) as! ShootKitChecklist

    SKC.job = job

    SKC.setValue(item, forKey: "item")
    SKC.setValue(used, forKey: "used")
    SKC.setValue(visible, forKey: "visible")

    do {
        try context.save()
        return (SKC, nil)
    } catch let error {
        print ("Failed to add Shoot Kit Item:", error)
        return (nil, error)
    }
}

}

当我尝试保存数据时,Job实体(第一个实体)将写入上下文,并且可以在另一个类中获取它。

我要保存到ShootKitChecklist的数据来自数组,因此我将setValues放入了外观。但是,似乎忽略了将任何数据保存到实体的操作。

var SKCequipment = ["A","B","C","D","E"]

@IBAction private func HandleSave(sender : UIButton) {



        let context = CoreDataManager.shared.persistentContainer.viewContext

        let job = NSEntityDescription.insertNewObject(forEntityName: "Job", into: context)
        let SKC = NSEntityDescription.insertNewObject(forEntityName: "ShootKitChecklist", into: context)

        job.setValue(jobBrandTextField.text, forKey: "jobBrand")
        job.setValue(jobNameTextField.text, forKey: "jobName")
        job.setValue(directorTextField.text, forKey: "directorName")
        job.setValue(agencyTextField.text, forKey: "agencyName")
        job.setValue(prodCoTextField.text, forKey: "prodCoName")


        for item in SKCequipment {
            print(item)
            SKC.setValue(item, forKey: "item")
            SKC.setValue(true, forKey: "used")
            SKC.setValue(true, forKey: "visible")
        }

        do {
            try context.save()
            dismiss(animated: true) {
                self.delegate?.didAddJob(job: job as! Job)
            }
        } catch let saveError {
            print("Failed to save company:", saveError)
        }
    }

要测试是否已将项目添加到核心数据中,我正在像这样提取项目:

guard let SKCitem = job?.skc?.allObjects as? [ShootKitChecklist] else { return}
self.skcitems = SKCitem
print(skcitems)

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

好的,我已经通过在协议中添加另一个功能并将Entity附加到委托中来修复它(我意识到,我以前并没有共享这个(抱歉)。这是我的新协议:

protocol NewJobControllerDelegate {
    func didAddJob(job : Job)
    func didAddSKC(SKC : ShootKitChecklist)
}

然后在HandleSave操作中,我将for循环更改为此:

for item in SKCequipment {
            let tuple = CoreDataManager.shared.createSKCItem(item: item, used: true, visible: true, job: job as! Job)
            if let error = tuple.1 {
                print("Cannot save items", error)
            } else  {
                self.delegate?.didAddSKC(SKC: tuple.0!)
            }

最后是我的代表提供的新功能:

func didAddSKC(SKC: ShootKitChecklist) {
        ShootKit.append(SKC)
    }

谢谢您的帮助!