Core Data - using Transient Properties in Group By

…衆ロ難τιáo~ 提交于 2020-02-14 06:41:11

问题


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.


回答1:


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.




回答2:


You can't involve transient properties in your fetch request, but you can use them for the section name key path, as long as they come out in the same order.

Try sorting your fetch request on lastName and firstName (two separate sort descriptors, in an array), then use player.fullName as the section name key path when creating your fetched results controller (just a and b from your list above).



来源:https://stackoverflow.com/questions/9359437/core-data-using-transient-properties-in-group-by

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!