What are Indexes in the Xcode Core-Data data model inspector

前端 未结 3 1976
野的像风
野的像风 2021-02-12 10:54

In Xcode you can add \"Indexes\" for an entity in the data model inspector.

\"Xcode

3条回答
  •  失恋的感觉
    2021-02-12 11:20

    Optimizing Core Data searches and sorts

    As the title says, indexing is to speed up searching and sorting your database. However it slows down saving changes to persistant store. It matters when you are using NSPredicate and NSSortDescriptor objects within your query.

    Let's say you have two entities: PBOUser and PBOLocation (many to many). You can see its properties at the image below:

    enter image description here

    Suppose that in database there is 10,000 users, and 50,000 locations. Now we need to find every user with email starting on a. If we provide such query without indexing, Core Data must check every record (basically 10,000).

    But what if it is indexed (in other words sorted by email descending)? --> Then Core Data checks only those records started with a. If Core Data reaches b then it will stop searching because it is obvious that there are no more records whose email starts with a since it is indexed.

    How to enable indexing on a Core Data model from within Xcode:

    enter image description here
    or:
    enter image description here

    Hopefully they are equivalent:-)

    But what if you wanted: Emails started with a and name starts with b You can do this checking INDEXED for name property for PBOUser entity, or:

    enter image description here

    This is how you can optimise your database:-)

提交回复
热议问题