我有两个不同的班级,一对多关系:
service docker stop
iptables -t nat -F POSTROUTING
ip link del docker0
service docker start
iptables -t nat -L -n # Check if POSTROUTING table is OK
保存FuelRecord对象时,Fuelrecord已链接到Car,但CarObject未链接到FuelRecord。
如您所见,该车与0 FuelRecords相关联
Click here to view Cars Table in Realm Browser
但FuelRecord包含Car引用
Click here to view FuelRecords Table in Realm Browser
由于问题,我无法获取特定汽车的燃油记录。
这就是我将FuelRecord保存到数据库的方式。
func AboutYoga() {
let width = mywebview.frame.width
let height = mywebview.frame.height
let frame = 0
mywebview.allowsInlineMediaPlayback = true
mywebview.scrollView.bounces=false
let videoUrl = "https://www.youtube.com/embed/36Q3V7WY3F0?rel=0&controls=1&&showinfo=0&modestbranding=1&playsinline=1"
let htmlUrl = "<html><body><iframe width=\(width) height=\(height) src=\(videoUrl) allowfullscreen></iframe></body></html>"
mywebview.loadHTMLString(htmlUrl, baseURL: Bundle.main.bundleURL)
}
谢谢!
答案 0 :(得分:1)
一对多关系是单向的。要自动查找与汽车相关的所有FuelRecords,从而使关系成双向,您应该使用反向关系。这可以通过LinkingObjects来实现。
将Car类中的FuelRecords声明更改为:
let FuelRecords = LinkingObjects(fromType: FuelRecord.self, property: "CarObject")
可悲的是,Realm Browser仍然没有向您显示反向关系(至少对我来说是这种情况),但您可以从代码中查询与汽车相关的所有FuelRecords。
答案 1 :(得分:0)
在您的代码中,它缺少您在FuelRecord对象中插入CarObject的部分。顺便说一句:我会在Car类中实现一个方法:
class Car: Object {
func addFuelRecordToCar(record : FuelRecord) {
self.FuelRecords.append(record)
}
}
在此之后,每当您创建燃油记录时:
fuelRecord.LiterPrice = Float(txtLiterPrice.text!)!
fuelRecord.Odometer = Float(txtOdometer.text!)!
fuelRecord.Total = Float(txtTotal.text!)!
fuelRecord.Volume = Float(txtVolume.text!)!
fuelRecord.IsPartial = switchPartial.isOn
// You missed how you insert the CarObject inside your FuelRecord
// I guess it's something like this
fuelRecord.CarObject = car
/* insert here */
let realm = try! Realm()
try! realm.write{
realm.add(fuelRecord)
在右侧部分插入以下代码:
car.addFuelRecordToCar(record: fuelRecord)
这应该有效。让我知道:))