简体   繁体   中英

Swift CoreData: Inserting instances of an Entity related to other 2 entities

I have an app with the following entities: Station, Program, and StationProgram :

Station <-->> StationProgram <<--> Program

Station refers to an electrovalve, Program is a watering program (there can be N programs in the database), and StationProgram has an attribute that indicates the time that a Station will water in a Program.

The point is that I can create correctly instances of Station and Program in the DB (in their respective View Controllers). However, I have a TableViewController where, for a given station selected in a previous controller, I want to show all the available programs, with a UISwitch indicating if this program has been associated to the station or not. Initially, there are no associations between stations and programs. The user can interact with all the existing programs in the DB and active them for this station (setting the UISwitch shown in the table row that points to the program to on). Finally, when the user wants to save the configuration, I want to insert the data in the table StationProgram. By now, to simplify, I just want to assign a time manually, for example, 2 minutes, to the programs active. I have the following code, but the execution crashes when I try to map this:

@IBAction func saveTapped(sender: AnyObject) {
// Reference to our app delegate
 let appDel: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate

 // Reference moc
 let context: NSManagedObjectContext = appDel.managedObjectContext!
 let en = NSEntityDescription.entityForName("Station", inManagedObjectContext: context)
 station.setValue(textFieldStationName.text as String, forKey: "name")

 let en2 = NSEntityDescription.entityForName("StationProgram", inManagedObjectContext: context)

 // The variable activePrograms contains the row of the table where this program is shown, and the wateringTime  
 for (selectedCellId,time) in activePrograms {
     var newStationProgramInstance = StationProgram(entity: en2, insertIntoManagedObjectContext: context)
     let program: NSManagedObject = programList[selectedCellId] as NSManagedObject
     // Map our properties
     newStationProgramInstance.wateringTime = time

     // station is a variable of type Station that is filled from the previous controller
     newStationProgramInstance.toStation = station as Station
     newStationProgramInstance.toProgram = program as Program
  }

  context.save(nil)
  self.navigationController.popViewControllerAnimated(true)

}

Specifically, the execution crashes at the line "newStationProgramInstance.toStation = station as Station". It says swift_dynamicCastClassUnconditional at

Thread 1: EXC_BREADKPOINT (code=EXC_I386_BPT, subcode=0x0)

Thank you very much for your help.

In Core Data you would model this kind of relationship with a many-to-many relationship, not a separate join table.

Station (programs) <<----->> (stations) Program

The only justifiable reason to use a join table is if you want to add and keep additional information about the relationship itself (such as dateCreated or similar). I doubt that is so in your case.

The creation of the relationship now becomes trivial. It is enough to just do it one way if the model is set up correctly with reverse relationships.

newStation.addProgramsObject(newProgram)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM