Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between transient and derived properties of a core data entity

What is the difference between transient and derived properties of a core data entity? I would like to create a "virtual" property that can be used in a fetch operation to return localized country names from a core data entity.

The operation is to be done this way:

  1. retrieve the country name from the database in english
  2. do a NSLocalizedString(countryNameInEnglish, nil) to obtain the localized country name.

2 is to be done by this "virtual" property.

Which one should I use? transient or derived and how do I do that?

I have nothing to show you because I have no clue what I should use.

thanks

like image 998
Duck Avatar asked Mar 18 '23 08:03

Duck


1 Answers

According to Apple's guide for Non-Standard Persistent Attributes:

You can use non-standard types for persistent attributes either by using transformable attributes or by using a transient property to represent the non-standard attribute backed by a supported persistent property. The principle behind the two approaches is the same: you present to consumers of your entity an attribute of the type you want, and “behind the scenes” it’s converted into a type that Core Data can manage. The difference between the approaches is that with transformable attributes you specify just one attribute and the conversion is handled automatically. In contrast, with transient properties you specify two attributes and you have to write code to perform the conversion.

I suggest using transient attributes. Idea is that you create 2 string attributes: countryName (non-transient) and localizedCountryName (transient):

How to set "transient" flag

And then, in your NSManagedObjectSubclass, you simply implement a getter for localizedCountryName:

- (NSString *)localizedCountryName
{
    NSString *result;

    if ([self.countryName length] > 0) {
        result = NSLocalizedString(self.countryName, nil);
    }

    return result;
}
like image 53
A.S. Avatar answered Apr 07 '23 13:04

A.S.