Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Average as a fetched property?

I'm not sure if I've understood Fetched Properties correctly, so do tell me if I'm going the wrong way with this and should be doing something else instead.

I have a little demo app where I have books that are being reviewed. A Review entity has a score property with the Int16 from 1 to 5 and a timestamp, and the Book entity has zero-to-many reviews through the isReviewed property (bookInReview as its inverse property).

I want to make a fetched property on book that is an average review score from the past 30 days. Can I use fetched properties in my model in Xcode 4 to do this? If so, how? I can tried setting Destination to Review and predicate to @avg(score) to start with all the reviews but that doesn't seem right.

Cheers

Nik

like image 606
niklassaers Avatar asked Jan 20 '26 17:01

niklassaers


1 Answers

Fetched Properties return an NSArray (actually an _NSFaultingArray, or something like that) of NSManagedObjects, so I don't believe it is possible to fetch aggregates like @sum, @average, or @count as fetched properties. However, that doesn't mean you cannot add them to a subclass of NSManagedObject or a category.

like image 68
sean woodward Avatar answered Jan 23 '26 19:01

sean woodward



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!