Database Reference
In-Depth Information
Primitive Access
Similar to the process of accessing attributes discussed earlier, changes to a
relationship will fire KVO notifications. Since there are situations in which we
do not want this to occur or in which we want a finer-grained control over
the notifications, there are primitive accessors for relationships as well.
However, there is no primitive method for retrieving an NSMutableSet for a to-
many relationship. Therefore, if the code requires changes to a relationship
with either delayed or no observations being fired, we must use -primitiveValue-
ForKey: to get back an NSSet , call -mutableCopy on the NSSet , add our new object
to the NSMutableSet , and finally use -setPrimitiveValue:forKey: to apply the changes.
Property Accessors
Relationships can use properties, just like the attributes discussed earlier.
In the code in Mutable Access of To-Many Relationships , on page 13 , if we
want to add a property to retrieve the relationship, we declare the following
property:
@property (strong) NSSet *recipeIngredients;
And then we flag it as dynamic in the implementation file.
Subclassing NSManagedObject
Although NSManagedObject provides a tremendous amount of flexibility and
handles the majority of the work a data object normally does, it does not
cover every possibility, and there are occasions where we might want to sub-
class it. Subclassing to gain @property access to attributes and relationships
is one common situation, but we may also want to add other convenience
methods or additional functionality to the object. When such a situation
arises, there are some general rules to remember.
Methods That Are Not Safe to Override
In Apple's documentation, the methods shown in Table 1, Methods never to
override , on page 15 should never be overridden.
It's quite a list. Most, if not all, of these methods are common sense, and
experience with Objective-C explains why they should not be overridden. Even
though this is a fairly long list, I'm going to add a few more.
-initXXX
The first is -initXXX . There is really no reason or benefit to overriding the -init
methods of an NSManagedObject , and there are situations in which doing so has
unpredictable results. Although it is not specifically against the documentation
 
 
Search WWH ::




Custom Search