Database Reference
In-Depth Information
dynamically at runtime, we do not need to declare them in the header. How-
ever, we do need to flag them as dynamic so that the compiler will not issue
a warning. This is done in the implementation file.
RecipesV2/PPRecipes/PPRRecipeMO.m
#import "PPRRecipeMO.h"
@implementation PPRRecipeMO
@dynamic desc;
@dynamic imagePath;
@dynamic lastUsed;
@dynamic name;
@dynamic serves;
@dynamic type;
By declaring them as @dynamic , we are telling the compiler to ignore any
warnings associated with these properties because we “promise” to generate
them at runtime. Naturally, if they turn up missing at runtime, our application
is going to crash. However, when we are working with NSManagedObject objects,
the attributes will be looked up for us, and we do not need to implement
anything. By adding that code, we can access the attribute directly, as shown
in the following example:
PPRecipe *myRecipe = ...;
NSString *recipeName = [myRecipe name];
//Do something with the name
[myRecipe setName:recipeName];
Primitive Access
It should be noted that accessing the attribute via KVC or properties will
trigger KVO notifications that the attribute has changed. There are situations
where we do not want this to occur or where we prefer it to occur later. In
those situations, we can access the attribute using the -primitiveValueForKey: and
-setPrimitiveValue:forKey: methods. Both of these methods work the same as the
-valueForKey: and -setValue:forKey methods that we are used to working with, but
they do not cause KVO notifications to fire. This means the rest of our appli-
cation will be unaware of any changes we make until and unless we notify it.
Where is this useful? I find it quite useful when I am loading in data from an
external source and the data is going to impact several attributes at once.
Imagine we wrote a recipe importer that accepted a comma-separated value
(CSV) file from another recipe application. In that situation, we might not
want the UI or other parts of our application making decisions based on the
data in the middle of the import. Therefore, we would want to update the
values without notifications, and once all of them are done, we let the notifi-
cations go out. The code to handle this situation would look like this:
 
 
 
Search WWH ::




Custom Search