3

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?

zombie
  • 5,069
  • 3
  • 25
  • 54

3 Answers3

2

I just finished a WWDC Core Data lab with Rishi who helped me with this! You should use sum:(items.totalPrice) instead of the .@sum syntax. The parentheses syntax can also be used for some other functions (e.g. count:(items) (the number of items in the to-many relationship) or max:(items.createdAt) (the date of the most recent item)).

halleygen
  • 481
  • 1
  • 7
  • 15
  • Thanks do you know if we nest these functions or go two levels deeper in the relationship – zombie Jun 11 '21 at 01:33
  • @zombie No, I don't think so. In the [WWDC19 talk](https://developer.apple.com/wwdc19/230) (around 19:58) where they introduce derived attributes they say "derivation expressions can refer to any of the properties of an entity, one level deep." – halleygen Jul 23 '21 at 09:49
0

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
pbasdf
  • 21,386
  • 4
  • 43
  • 75
  • the expression of the item total written as `price * amount` throws an exception on creating the store container `exception: currently unsupported (too many keypaths)` do you think this is because of the same reason and where can I find more on how to wrote the correct syntax – zombie Jul 08 '20 at 13:12
  • When I do that for a sum, I got a ```*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'currently unsupported (unsupported function on to many (not count or sum))'```. Any idea why? – alpennec Nov 09 '20 at 10:30
0

Use items.totalPrice.@sum as the derived property's expression in Xcode's model editor.

This only looks to work for numeric types though? I have a property maxDate with a derived property expression of

items.createdAt.@max

It compiles but throws an error at runtime:

'NSInvalidArgumentException', reason: 'currently unsupported (too many steps)

Where Date is the data type for createdAt

spnkr
  • 952
  • 9
  • 18
  • I saw that exception multiple times that I couldn’t update my code to use it I hope this will be improved – zombie Jul 26 '20 at 02:03
  • ```@max``` is not supported. Only ```@count``` and ```@sum```. But when I try to do ```items.totalPrice.@sum```, I have the following crash: ```*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'currently unsupported (unsupported function on to many (not count or sum))'```. Any idea why? – alpennec Nov 09 '20 at 10:43