Greg
Greg

Reputation: 34798

why would I need to use a primitive accessor methods in a core data project?

Why would I need to use a primitive accessor methods in a core data project?

I'm reading about Core Data and note the below:

By default, Core Data dynamically creates efficient public and primitive get and set accessor methods for modeled properties (attributes and relationships) of managed object classes...

For example, given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstName:, primitiveFirstName, and setPrimitiveFirstName:.

I'm still not sure what the primitive accessor methods are? How do they work? When & why would I need to use them over the normal accessor methods?

thanks

Upvotes: 3

Views: 1916

Answers (3)

TechZen
TechZen

Reputation: 64428

The primary use of primitive accessors is to prevent key-value observing notifications from being sent when you change a value. Sometimes you do not want such notifications sent because they have quite a bit of overhead. E.g. when importing large sets of data or when you use a transitory value to alter a persisted value.

You use them almost always when writing customer accessor methods. If you look at the accessor methods generated by Xcode for managed object subclasses, you can see how they are used.

Upvotes: 1

paulbailey
paulbailey

Reputation: 5346

If you are writing your own methods in a subclass of NSManagedObject, the primitive accessors enable you to get directly at the data the object contains.

Effectively, you would use them the same way you would use an instance variable in a normal class.

Upvotes: 1

ughoavgfhw
ughoavgfhw

Reputation: 39905

In normal classes, you would not usually use primitive accessors, but Core Data uses them frequently. The most common scenario: You write a custom accessor (to perform maintenance, create a default object, handle transient wrapper of persistent property, etc.) but want to use Core Data's optimized storage. If the optimized accessors were normal accessors, your custom one would prevent it from being available, but instead you just have to use the primitive accessor.

Also, the default accessors you get from Xcode include methods to add or remove objects from to-many relationships. If these methods used normal accessors, the normal accessors would trigger change notifications for the entire set. Instead they use the primitive accessors and create their own change notifications for just the part they are changing.

The most important thing is that the primitive accessors are the only way to get to the optimized storage. Without them, you would have to use instance variables, which Apple advises against.

Upvotes: 5

Related Questions