How to sort based on a calculated value from multiple Core Data attributes

♀尐吖头ヾ 提交于 2019-12-08 00:38:51

问题


I'm trying to create a sort descriptor, for a core data fetch request, that will fetch objects that are ordered by a calculated value (e.g., the sum of two or more attributes). Is this possible? Thanks in advance.


回答1:


From the "Core Data Programming Guide":

You cannot fetch using a predicate based on transient properties (although you can use transient properties to filter in memory yourself). ... To summarize, though, if you execute a fetch directly, you should typically not add Objective-C-based predicates or sort descriptors to the fetch request. Instead you should apply these to the results of the fetch.

So sorting by a calculated value seems not to be possible. You have to pre-calculate the sorting value and store it as (non-transient) attribute in your objects.



来源:https://stackoverflow.com/questions/11927762/how-to-sort-based-on-a-calculated-value-from-multiple-core-data-attributes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!