Write native SQL in Core Data

前端 未结 3 919
别那么骄傲
别那么骄傲 2021-01-22 19:15

I need to write a native SQL Query while I\'m using Core Data in my project. I really need to do that, since I\'m using NSPredicate right now and it\'s not efficien

3条回答
  •  难免孤独
    2021-01-22 19:47

    Short answer: you can't do this.

    Long answer: Core Data is not a database per se - it's not guaranteed to have anything relational backing it, let alone a specific version of SQLite that you can query against. Furthermore, going mucking around in Core Data's persistent store files is a recipe for disaster, especially if Apple decides to change the format of that file in some way. You should instead try to find better ways to optimize your usage of NSPredicate or start caching the values you care about yourself.

    Have you considered using the KVC collection operators? For example, if you have an entity Foo each with a bunch of children Bar, and those Bars have a Baz integer value, I think you can get the sum of those for each Foo by doing something like:

    foo.bars.@sum.baz
    

    Not sure if these are applicable to predicates, but it's worth looking into.

提交回复
热议问题