核心数据 - 在 Group By 中使用瞬态属性

发布于 2025-01-07 14:26:58 字数 647 浏览 0 评论 0原文

我正在创建一个带有一些聚合数据的 UITableView。在此过程中,需要使用节标题对表视图单元格进行排序和分组。

问题是我想在 NSFetchRequest 中使用瞬态属性来生成节标题和标题。结果排序。问题是,在设置 NSFetchRequest 时,我收到“NSInvalidArgumentException”,原因是:“传递给 setPropertiesToFetch 的密钥路径player.fullName 无效”。

NSFetchRequest 的主要实体是一个具有以下属性的 Player 实体:firstName 和 name。姓。为了对数据进行排序和分组,引入了瞬态属性“fullName”。这是lastName 和firstName 属性的简单串联。

到目前为止尝试过的事情是:

a) 定义 -(NSString*)fullName 方法

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

c) 添加 @dynamic fullName

d) 将 fullName 属性添加到 Player 实体 &使其变得短暂。

是否有任何想法或无法在包含 group by 子句的 NSFetchRequest 中选择瞬态属性。

任何帮助表示赞赏。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一笑 2025-01-14 14:26:58

最后,似乎在 NSFetchResults 和 Group By 的组中包含瞬态属性是不可能的。

jrturton 提出的很棒的建议已经接近。最后,瞬态属性 fullName 很容易在实体更新时生成,并且更新频率非常低,因此解决方案是停止使用瞬态属性并创建一个完全成熟的属性。只需将其视为极端的非规范化即可。

解决方案是设置以下内容,

-(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 实体的属性并消除我的问题。希望有帮助。

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.

可爱暴击 2025-01-14 14:26:58

您不能在提取请求中包含瞬态属性,但您可以将它们用于部分名称键路径,只要它们以相同的顺序出现即可。

尝试对lastName和firstName(两个单独的排序描述符,在一个数组中)排序您的提取请求,然后在创建提取的结果控制器时使用player.fullName作为部分名称键路径(仅来自a和b)您上面的清单)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文