Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data Transient Calculated Attributes

I have an entity that contains lastName and firstName attributes. For reasons beyond the scope of this question, I want a fullName attribute that gets calculated as a concatenation of firstName + space + lastName.

Because this is purely a calculated value, with no need for redo/undo or any other of the more sophisticated aspects of transient attributes (merging, etc.), my gut tells me to just override the getter method to return said calculated value. Reading suggests that, if I do this, my only concern would be whether it's KVO compliant, which I can address by using keyPathsForValuesAffectingVolume to ensure changes to firstName or lastName trigger notifications for anyone observing on fullName.

Am I missing anything? I'm checking because I'm a beginner to this environment.

like image 559
wayne Avatar asked May 17 '11 16:05

wayne


People also ask

What is transient property in Core Data?

Transient attributes are properties that you define as part of the model, but that are not saved to the persistent store as part of an entity instance's data. Core Data does track changes you make to transient properties, so they are recorded for undo operations.

What is transient property in Swift?

Transient means that the value is stored in memory on the object graph.

What is transformable type in Core Data?

Core Data supports entity attributes of 'Transformable' type. Transformable type allows us to store custom data types as an object of the declared attribute for a record of an Entity. The only requirement is that our custom type should conform to NSCoding or we need to provide a custom value transformer.

What is @NSManaged?

@NSManaged indicates that the variables will get some values when we run the app. Coredata automatically creates getter and setter for such props. It silences the compiler for warnings. NSmanaged is subclass of NSObject. @NSManaged means extra code will be given to these props at runtime.


1 Answers

I'm also new to this, so I'm not completely sure about my answer, but as I understand it you are correct.

- (NSString *)fullName
{
    [self willAccessValueForKey:@"fullName"];
    NSString *tmp = [self primitiveFullName];
    [self didAccessValueForKey:@"fullName"];

    if (!tmp) {
        tmp = [NSString stringWithFormat:@"%@ %@", [self firstName], [self lastName]];
        [self setPrimitiveFullName:tmp];
    }
    return tmp;
}

- (void)setFirstName:(NSString *)aFirstName
{
    [self willChangeValueForKey:@"firstName"];
    [self setPrimitiveFirstName:aFirstName];
    [self didChangeValueForKey:@"firstName"];

    [self setPrimitiveFullName:nil];
}

- (void)setLastName:(NSString *)aLastName
{
    [self willChangeValueForKey:@"lastName"];
    [self setPrimitiveLastName:aLastName];
    [self didChangeValueForKey:@"lastName"];

    [self setPrimitiveFullName:nil];
}

+ (NSSet *)keyPathsForValuesAffectingFullName
{
    return [NSSet setWithObjects:@"firstName", @"lastName", nil];
}
like image 176
Mark Leonard Avatar answered Sep 30 '22 12:09

Mark Leonard