Bhushan B
Bhushan B

Reputation: 2510

Fetch and combine two attributes that belong to a specific entity

I have an User entity. It has many attributes like firstName and lastName. I have to fetch user's full name (firstName + lastName). I need it as a single NSString.

Is it possible?

Upvotes: 1

Views: 466

Answers (1)

Lorenzo B
Lorenzo B

Reputation: 33428

The cleanest way to do it, IMHO, is the following.

Create a User class, subclass of NSManagedObject.

Create fullName attribute (of type NSString) in your entity as transient (it will not be stored on disk).

Then override awakeFromFetch method to set the fullName as a concatenation of firstName and lastName like the following:

self.fullName = [NSString stringWithFormat:@"%@ %@",
                            self.firstName, self.lastName];

Here the link to the Core Data doc. In particular, take a look at Object Life-Cycle—Initialization and Deallocation.

Hope that helps.

Upvotes: 1

Related Questions