iosswiftcore-dataexpressionderiveddata

core data derivation expression key path uses an operator as an intermediate component


I'm trying to write the derivation expression for the sum of a to many relationship attribute.

I have an item and a group, the item has a price and total price (amount * price). I want to write an expression for the total price for the group as the sum of its components.

enter image description here

When I build I get the error

error: Misconfigured Property: LAEItemGroup.totalPrice key path “items.@sum.totalPrice” uses an operator as an intermediate component

according to the documentation and the WWDC 2019 Making Apps with Core Data it should be possible to get the sum on a to many relationship.

Could someone please help me find the correct syntax or way to do so.

As a work around I tried to write a var that worked in that class as so

@objc
public var totalPrice: Double {
    value(forKeyPath: "items.@sum.totalPrice") as? Double ?? 0
}

so why the KeyPath value works but not in the model editor?


Solution

  • I've now had an opportunity to check. It seems the format used by the model editor is for the aggregate operator to be at the end of the expression (which as you point out, is different from the format used in other expressions):

    items.totalPrice.@sum