I have a table in Realm Database .
for example factor with three field:
id,primerykey
gprice
gcount
i want calculate sum(gprice * gcount) in all of rows RealmResult.
how can write a query for aggregation and calculate this relation.
i write below query but i want sum(gprice*gcount).
double sum=rows.sum(FactorRows.COLUMN_gprice).doubleValue();
sum is only gprice.
many times search in realm docs but not found anything.
thanks a lot.
Similarly to this question, predicates like this one don't exist in the RealmQuery, but you can store a third field that stores gprice*gcount for each element, and then call sum() for that third field.
this is very very bad for Realm why?, it isn't normal method for design database.
You can redefine the setters of those two properties to automatically modify the third field whenever you modify those two. The additional required logic for this solution to work is minimal. (two lines of code)
i think this is not a true solution and standard and need to a operation in query,
Ops!! Thanks
@jjorian we have prioritised to make features that has no workarounds and with a high demand. This has a very easy workaround and you are actually the first to ask for it :-)
BUT that said, the core database does actually support this! So it's mostly a matter of defining the API for it :-) We just haven't gotten around to it for the first reasons mentioned.
@bmunkholm Realm is very Likable For me ,I just told my opinions and you have answered well.
I've met two weeks Realm And I intend to continue with further work on it.
thanks.
why realm simply doesn't support Function<T, U>? I can overcome issues like this but those workarounds are not the most elegant way. (not talking about performance, but about how the code looks. 😺 ) @Zhuinden
Most helpful comment
i think this is not a true solution and standard and need to a operation in query,
Ops!! Thanks