Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3 - Core Data Saving with one-to-many relationship

I'm working with core data and i have two entity:

SwimmingPool Parameters Each swimming pool can have multiple parameters

relation between swimming pool and parameters

swimming pool entity

parameters entity

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!

like image 948
Marco Avatar asked Oct 29 '25 16:10

Marco


2 Answers

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()
like image 110
Irshad Ahmad Avatar answered Oct 31 '25 06:10

Irshad Ahmad


If you want to add Parameters to a specific SwimmingPool

  • In the segue pass the specific SwimmingPool instance to the form controller.
  • Create Parameters instances by inserting them into the context.
  • Add the relationship to the SwimmingPool instance by calling insert on the parameter attribute / relationship or the generated accessor addToParameter of the NSManagedObject subclass.
  • Save the context. (important as last step).

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

like image 41
vadian Avatar answered Oct 31 '25 05:10

vadian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!