Search code examples
iosswiftcore-data

Saving into multiple entities - Core Data - Swift


Hopefully this will make sense.

I am trying to store data into 2 different entities from one function but not having much luck with storing data into the second entity.

My 2 entities are called - Job & ShootKitChecklist.

I have created a core data manager, I have stripped this back to just show you the section I am using:

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)
    }
}

}

When I try to save the data, the Job entity (First Entity)writes to the context and I can fetch it in another class.

The data I am trying to save to the ShootKitChecklist is from an array so I put my setValues into a for look. However, it seems to ignore saving any data to the entity.

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)
        }
    }

To test to see if the items have been added to the core data I am fetching the items like this:

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

Thank you in advance, huge help!


Solution

  • Okay I have fixed it by adding another function to my protocol and appending the Entity in the delegate(I realise that I didn't share this before (apologies). Here is my new protocol:

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

    And then in my HandleSave action I changed my for loop to this:

    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!)
                }
    

    And finally my new function from my delegate:

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

    Thank you for your help!