Core data CoreData:关键值观察关系NSSet元素';s属性

Core data CoreData:关键值观察关系NSSet元素';s属性,core-data,key-value-observing,nsoutlineview,Core Data,Key Value Observing,Nsoutlineview,我有一个车库实体,其中包含多辆汽车(一对多关系)。 汽车具有price:NSNumber属性 我通过NSTreeController在NSOutlineView中显示数据。 一组行表示一个车库,其标签的值是车库中所有汽车的总值 组中的一行表示一辆车 当我更改汽车的价格(通过编辑或单击按钮)时,会更新汽车行,但不会更新车库组行。但是,当我添加或删除一辆汽车时,车库行中的总价值会得到更新 以下是我的课程: public class Garage: NSManagedObject { @NS

我有一个
车库
实体,其中包含多辆
汽车
(一对多关系)。 汽车具有
price:NSNumber
属性

我通过NSTreeController在NSOutlineView中显示数据。 一组行表示一个
车库
,其标签的值是车库中所有汽车的总值

组中的一行表示一辆车

当我更改汽车的价格(通过编辑或单击按钮)时,会更新汽车行,但不会更新车库组行。但是,当我添加或删除一辆汽车时,车库行中的总价值会得到更新

以下是我的课程:

public class Garage: NSManagedObject {

    @NSManaged public var cars: NSSet?    

    public override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
        let mutableSet = NSMutableSet(set: super.keyPathsForValuesAffectingValue(forKey: key))
        if key == "totalValueGarage" {
            mutableSet.add("cars")
        }
        return mutableSet as! Set<String>
    }
}
    /// Total value of the garage. Bound to the NSOutlineView group row.
    @objc dynamic var totalValue: NSNumber {
        var toReturn: NSNumber = NSNumber(value: 0.0)
        if let cars = self.cars {
            for case let car in cars {
                toReturn = NSNumber(value: (toReturn.doubleValue + car.price.doubleValue))               
            }
        }
        return toReturn
    }
公共类车库:NSManagedObject{
@NSV管理的公共var车辆:NSSet?
公共重写类func keypathsforvaluesafectingvalue(forKey:String)->Set{
设mutableSet=NSMutableSet(set:super.keypathsforvaluesafectingvalue(forKey:key))
如果键==“totalValueGarage”{
可变集合。添加(“汽车”)
}
将mutableSet返回为!Set
}
}
///车库的总价值。绑定到NSOutlineView组行。
@objc动态var总值:NSNumber{
var toReturn:NSNumber=NSNumber(值:0.0)
如果let cars=self.cars{
例如,让汽车进入汽车{
toReturn=NSNumber(值:(toReturn.doubleValue+car.price.doubleValue))
}
}
返回返回
}
公共级汽车:NSManagedObject{
@NSV管理的公共var价格:NSNumber?
@NSVAR管理的公共车库:车库?
公共重写类func keypathsforvaluesafectingvalue(forKey:String)->Set{
设mutableSet=NSMutableSet(set:super.keypathsforvaluesafectingvalue(forKey:key))
如果键==“价格”{
mutableSet.add(“车库”)//让系统知道车库受价格值的影响
}
将mutableSet返回为!Set
}
}
如何确保在更改汽车价格时更新大纲视图的组行

我尝试的

  • 手动调用garage.willChangeValueForKey(“汽车”)和garage.willChangeValueForKey(“汽车”)。这很难看,撤消更改不会传播更改。我也必须在撤消时触发这些通知
  • 调用outlineView.reloadItem(item:Any?),但它不起作用。此外,它没有使用KVO那么优雅
谢谢你的建议

最后我死了。我想避免让家长不得不按程序观察每个孩子。相反,每个孩子都知道自己的父母,只要在父母身上调用一个(虚假的)setter就可以触发重新计算。我想避免让家长不得不按程序观察每个孩子。相反,每个子级都知道自己的父级,只需调用父级上的(虚假)setter即可触发重新计算。
public class Car: NSManagedObject {

    @NSManaged public var price: NSNumber?
    @NSManaged public var garage: Garage?

    public override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
        let mutableSet = NSMutableSet(set: super.keyPathsForValuesAffectingValue(forKey: key))
        if key == "price" {
            mutableSet.add("garage") // Let the system know that garage is affected by the price value
        }
        return mutableSet as! Set<String>
    }

}