核心数据 - 使用组中的瞬态属性 [英] Core Data - using Transient Properties in Group By

查看:122
本文介绍了核心数据 - 使用组中的瞬态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些聚合数据创建一个UITableView。一路上,Section标题需要用于排序和分组表视图单元格。

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.

问题是我想在NSFetchRequest中使用瞬态属性生成部分标题&结果排序。问题是,虽然设置NSFetchRequest我收到一个NSInvalidArgumentException,原因:'无效的keypath player.fullName传递给setPropertiesToFetch'。

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'.

NSFetchRequest是一个具有属性的播放器实体:firstName&姓。为了对数据进行排序和分组,已经引入了临时属性fullName。这是一个简单的lastName和firstName属性的连接。

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.

到目前为止的尝试是:

定义 - (NSString *)fullName方法

a) Defining the -(NSString*)fullName method

b)定义一个@property(nonatomic,readonly)NSString * fullName

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

c)添加@dynamic fullName

c) Adding a @dynamic fullName

d)将fullName属性添加到播放器实体&

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

有没有任何想法,或者现在可以在包含group by子句的NSFetchRequest中选择临时属性。

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

任何帮助赞赏。

推荐答案

到底是由NSFetchResults与Group By是不可能的。

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

jrturton的建议非常接近。最后,瞬态属性fullName很容易在更新到实体时生成,并且只是很少更新,因此解决方案是停止使用瞬态属性并创建完全属性。只要将其视为极端反规范化。

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.

解决方案是设置以下

-(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" ];
}

这会将fullName作为Player实体的属性更新并删除我的问题。希望它有帮助。

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

这篇关于核心数据 - 使用组中的瞬态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆