简体   繁体   English

Swift CoreData:插入与其他2个实体相关的Entity的实例

[英]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,Program和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. Station是指一个电动阀,Program是一个浇水程序(数据库中可以有N个程序),StationProgram具有一个属性,该属性指示Station将在程序中浇水的时间。

The point is that I can create correctly instances of Station and Program in the DB (in their respective View Controllers). 关键是我可以在DB中(在各自的View Controller中)正确创建Station和Program的实例。 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. 但是,我有一个TableViewController,对于先前控制器中选择的给定工作站,我想显示所有可用程序,并带有UISwitch指示此程序是否已与工作站关联。 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). 用户可以与数据库中的所有现有程序进行交互,并为该工作站激活它们(将指向程序的表行中显示的UISwitch设置为on)。 Finally, when the user wants to save the configuration, I want to insert the data in the table StationProgram. 最后,当用户想要保存配置时,我想将数据插入表StationProgram中。 By now, to simplify, I just want to assign a time manually, for example, 2 minutes, to the programs active. 现在,为简化起见,我只想为活动的程序手动分配一个时间,例如2分钟。 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". 具体来说,执行会在“ newStationProgramInstance.toStation =站点作为站点”行中崩溃。 It says swift_dynamicCastClassUnconditional at 它说swift_dynamicCastClassUnconditional at

Thread 1: EXC_BREADKPOINT (code=EXC_I386_BPT, subcode=0x0) 线程1:EXC_BREADKPOINT(代码= EXC_I386_BPT,子代码= 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. 在Core Data中,您将使用多对多关系而不是单独的联接表来对这种关系进行建模。

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). 使用联接表的唯一正当理由是,您是否要添加并保留有关关系本身的其他信息(例如dateCreated或类似内容)。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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