Nuno Dias
Nuno Dias

Reputation: 3948

What is the need for a primitive when using a transient property?

What is it?

I'm not sure I quite understand what this does.

- (NSString *)sectionIdentifier {

    [self willAccessValueForKey:@"sectionIdentifier"];
    NSString *tmp = [self primitiveSectionIdentifier];
    [self didAccessValueForKey:@"sectionIdentifier"];

    if (!tmp) {

        tmp = @"bananas";

        [self setPrimitiveSectionIdentifier:tmp];
    }
    return tmp;
}

How come I need this primitiveSectionIdentifier?

Ultimately, I'm using a example project from Apple's documentation to create a section identifier, to use with my NSFetchedResultsController.

While this does work. I am saying to myself that,

"sectionIdentifier" will be accessed, then I'm setting "tmp" to primitiveSectionIdentifier. But primitiveSectionIdentifier has nothing there at this point!! Does it?

I then say I did access "sectionIdentifier". But I can't see how that happened between "Will" and "Did"!

Can someone help me understand this?

Upvotes: 2

Views: 182

Answers (1)

Martin R
Martin R

Reputation: 539925

[self primitiveSectionIdentifier] is a so-called "primitive accessor" (see the Glossary of the Core Data Programming Guide). That is the function that actually fetches the value of the "sectionIdentifier" from the persistent store. The function is automatically created by the Core Data runtime.

willAccessValueForKey and didAccessValueForKey are "notification methods". According to the documentation, they are used for key-value observing, maintaining inverse relationships and so on.

So the pattern is:

  1. Tell everybody that you are going to read a value.
  2. Read the value.
  3. Tell everybody that you have read the value.

Upvotes: 3

Related Questions