Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data - using Transient Properties in Group By

I'm creating a UITableView with some aggregated data. Along the way, Section Headings need to be used in sorting and grouping the table view cells.

The issue is I would like to use a Transient Property within the NSFetchRequest to generate section headings & results sorts. The issue is that whilst setting up the NSFetchRequest I receive a ''NSInvalidArgumentException', reason: 'Invalid keypath player.fullName passed to setPropertiesToFetch'.

The main entity for the NSFetchRequest is a Player entity with to properties: firstName & lastName. To sort and group the data a transient property 'fullName' has been introduced. This is a simple concatenation of the lastName and firstName properties.

Things tried so far are:

a) Defining the -(NSString*)fullName method

b) Defining a @property (nonatomic,readonly) NSString *fullName

c) Adding a @dynamic fullName

d) Adding the fullName attribute to the Player entity & making it transient.

Are there any ideas or is there noway to select transient properties in a NSFetchRequest that includes a group by clause.

Any help appreciated.

like image 559
user1220717 Avatar asked Feb 20 '12 10:02

user1220717


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. Computed means that the value is stored nowhere and is calculated in the getter. Both are distinct from the classic non-transient attribute which is stored on the object graph and is saved to disk.


1 Answers

Well in the end it seems including a transient property in a group by NSFetchResults with a Group By is not possible.

Great suggestion by jrturton got close. In the end, the transient property fullName was easy enough to generate on update to the entity and only updated very infrequently so the solution was to stop using a transient property and make a fully fledged attribute. Just think of it as extreme denormalisation.

the solution was to setup the following

-(void)setLastName:(NSString*)aName
{
    [self willChangeValueForKey: @"lastName" ];
    [self setPrimitiveValue: aName forKey: @"lastName" ];
    [self updateFullName];
    [self didChangeValueForKey: @"lastName" ];
}

-(void)setFirstName:(NSString*)aName
{
    [self willChangeValueForKey: @"firstName" ];
    [self setPrimitiveValue: aName forKey: @"firstName"];    
    [self updateFullName];
    [self didChangeValueForKey: @"firstName" ];
}

This updates the fullName as a property of the Player entity and removed my issues. Hope it helps.

like image 198
user1220717 Avatar answered Nov 15 '22 09:11

user1220717