Question

I'm starting to create an application with Core Data, to retrieve a data for sectioned table i want to use NSFetchedResultController, in the example from apple there are two additional properties.

  • primitiveTimeStamp
  • primitiveSectionIdentifier

For the case of primitiveSectionIdentifier apple says that

In contrast, with transient properties you specify two attributes and you have to write code to perform the conversion.

because the sectionidentifier is transient property. But what about the timeStamp ?this attribute is not a transient, why there is a primitiveTimeStamp property ? and why there is explicit setter for timeStamp ?

- (void)setTimeStamp:(NSDate *)newDate {

    // If the time stamp changes, the section identifier become invalid.
    [self willChangeValueForKey:@"timeStamp"];
    [self setPrimitiveTimeStamp:newDate];
    [self didChangeValueForKey:@"timeStamp"];

    [self setPrimitiveSectionIdentifier:nil];
}

or maybe it's not a actual setter? where is _timeStamp=newDate?

Était-ce utile?

La solution

CoreData generates the accessors for you. It generates "public and primitive get and set accessor methods for modeled properties".

So in this case it has generated:

-(NSDate*)timeStamp;
-(void)setTimeStamp:;
-(NSDate*)primitiveTimeStamp;
-(void)setPrimitiveTimeStamp:;

"why there is a primitiveTimeStamp property ?"

The declaration is merely to suppress compiler warnings. ie. If you removed the declaration of the property you'd find a warning on compilation but the code would still run. Or alternatively you could use [self setPrimitiveValue:newDate forKey:@"timeStamp"];

"why there is explicit setter for timeStamp ?"

This is required since setting the timeStamp requires the 'sectionIdentifier' to be recalculated. This is achieved by setting it no nil and letting the get accessor recalculate it lazily.

"where is _timeStamp=newDate?"

The equivalent of this is essentially done in the auto generated implementation of setPrimitiveTimeStamp.

A quote from the docs:

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. This includes the key-value coding mutable proxy methods such as addObject: and removes:, as detailed in the documentation for mutableSetValueForKey:—managed objects are effectively mutable proxies for all their to-many relationships.

Note: If you choose to implement your own accessors, the dynamically-generated methods never replace your own code. For example, given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstName:, primitiveFirstName, and setPrimitiveFirstName:. Core Data does this even for entities represented by NSManagedObject. To suppress compiler warnings when you invoke these methods, you should use the Objective-C 2.0 declared properties feature, as described in “Declaration.”

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top