I have an app with multiple coreData entities. The user can connect them. For example: a company can be connected to a employee.
But previously i only got the connecting to work on the detail view, not on the add view. Because the entitie has to be created first. I thought that i found a way to get this to work. But the problem is that it works sometime and not always.
This is the function that creates an entity:
func addJob(rating: Int16, jobTitle: String, jobListing: String, location: String) {
let newJob = JobEntity(context: manager.context)
newJob.jobTitle = jobTitle
newJob.jobListing = jobListing
newJob.location = location
save()
setJobCounter()
EntityConnector.instance.addNewJobToCompany(companyIndex: companyIndex, vm: CoreDataViewModel())
}
The last line calls a function that connects the entities. This is the class:
import Foundation
class EntityConnector {
static let instance = EntityConnector()
func addNewJobToCompany(companyIndex: Int, vm: CoreDataViewModel) {
let jobIndex = vm.getJobCount() - 1
let currentCompany = vm.companies[companyIndex]
currentCompany.addToJobs(vm.jobs[jobIndex])
vm.save()
}
func addContactToCompany(companyIndex: Int, vm: CoreDataViewModel) {
let contactIndex = vm.getContactCount() - 1
let currentCompany = vm.companies[companyIndex]
currentCompany.addToContacts(vm.contacts[contactIndex])
vm.save()
}
}
Does anyone have any idea how to solve this?