I'm working with core data and i have two entity:
SwimmingPool Parameters Each swimming pool can have multiple parameters



I have already saved and fetched data for swimming pool.
I've saved them trough a form.
In my AppDelegate i have:
let ad = UIApplication.shared.delegate as! AppDelegate
let context = ad.persistentContainer.viewContext
and here's the func:
 var swimmingpool: SwimminPool!
    if swimmingPoolToEdit == nil {
        swimming = SwimminPool(context: context)
    } else {
        swimming = swimmingPoolToEdit
    }
    if let name =  swimmingName.text {
        swimming.name = name
    }
    if let volume = swimmingVolume.text {
        swimming.volume = volume
.........................................................
ad.saveContext()
and i perform the fetch like so:
var controller: NSFetchedResultsController<SwimmingPool>!
func attemptFetch() {
    let fetchRequest: NSFetchRequest<SwimminPool> = SwimminPool.fetchRequest()
    let dataSort = NSSortDescriptor(key: "createdAt", ascending: false)
    fetchRequest.sortDescriptors = [dataSort]
     let controller = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
    controller.delegate = self
    self.controller = controller
    do {
        try controller.performFetch()
    } catch {
        let error = error as NSError
        print("\(error.debugDescription)")
    }
}
Through a segue i pass all the data to a new controller and through a new form (in an another controller) i'm trying to save the data for the parameters and connect them to the the specific swimming pool.
Do i need to save the data like i did for the swimming pool and the relationship will automatically connect the two entities or do i need to connect them upon saving?
I'm new to swift and i'm not sure how to save related datas
The connection will be made upon saving or when i will perform the parameters fetch?
Thank you!
Create Parameter managed object and then link this parameter to a SwimingPool managed object:
let parameter = Parameters(context: context)
parameter.chlorum = Double(12245454.12211) // insert your value
parameter.oxigen = "your value here"
parameter.createdAt = "your value here"
let swimming = SwimminPool(context: context)
swimming.parameter = parameter
ad.saveContext()
If you want to add Parameters to a specific SwimmingPool 
SwimmingPool instance to the form controller.Parameters instances by inserting them into the context.SwimmingPool instance by calling insert on the parameter attribute / relationship or the generated accessor addToParameter of the NSManagedObject subclass.Note : To avoid confusion name an Entity always in singular form (e.g. SwimmingPool / Parameter), a one-to-one relationship also in singular form (e.g. parameter) and  a one-to-many relationship in plural form (e.g. parameters)
PS: You question is ambiguous: You are writing Each swimming pool can have multiple parameters but the model design is Each parameter can have multiple swimming pools
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With