Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

Xcode sidebar showing the Indexes view

For the screenshot I did hit "add" twice so "comma,separated,properties" is just the default value.

What exactly are those indexes?
Do they have anything to do with indexed attributes? And if they have what is the difference between specifying the Indexes in this inspector and selecting "Indexed" for the individual attribute?

like image 362
Matthias Bauch Avatar asked Dec 19 '11 14:12

Matthias Bauch


People also ask

What are relationships in Core Data?

Inverse relationships enable Core Data to propagate change in both directions when an instance of either the source or destination type changes. Every relationship must have an inverse. When creating relationships in the Graph editor, you add inverse relationships between entities in a single step.

What is Xcode Core Data?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

What is an entity in Core Data?

An entity describes an object, including its name, attributes, and relationships.

What is transient in Core Data?

Transient attributes are properties that you define as part of the model, but that are not saved to the persistent store as part of an entity instance's data. Core Data does track changes you make to transient properties, so they are recorded for undo operations.


1 Answers

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:-)

like image 146
Bartłomiej Semańczyk Avatar answered Sep 18 '22 16:09

Bartłomiej Semańczyk