是否可以在核心数据查询中仅获取选定的属性 [英] Is it possible to fetch only selected property in a Core Data query

查看:13
本文介绍了是否可以在核心数据查询中仅获取选定的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有(我猜)典型的问题.我的 Core Data 数据库包含表,其中包含 BLOB 字段和相当长的文本字段.

I have (I guess) typical problem. My Core Data database contains table, which have BLOB fields and quite long text fields.

通常我不需要加载那些大尺寸的字段.由于数据库中有数千条记录,我更愿意只获取我真正需要的数据.换句话说,我想进行SELECT name, id FROM TAB_NAME"查询,而不是SELECT * FROM TAB_NAME"查询.

Usually I do not need to load agrresivly those large size fields. Since there are several thousand records in a database I would prefer to fetch only data I truly need. In other words I would like to make "SELECT name, id FROM TAB_NAME" query, not "SELECT * FROM TAB_NAME" query.

有什么方法可以使用 Core Data 只获取选定的字段?或者我应该寻找其他选项,例如,将大字段分开到另一个表?也许有更好(更简单)的方法来做到这一点?

Is there any way to fetch only selected fields using Core Data? Or shall I look for some other option, like, for instance, seperate large fields to another table? Maybe there is a better (easier) way to do this?

我习惯于使用 Hibernate 或 JPA,在这些地方很容易完成上述操作,但在阅读 Core Data 文档后,我没有看到这样的选项.

I am used to working with Hibernate or JPA, where it easy to do described above operation, after going through Core Data docs I do not see such option.

推荐答案

换句话说,我想做SELECT name, id FROM TAB_NAME"查询,而不是SELECT * FROM TAB_NAME"查询.

In other words I would like to make "SELECT name, id FROM TAB_NAME" query, not "SELECT * FROM TAB_NAME" query.

这个问题的答案已被接受,但仅供参考,以下是如何仅获取具有核心数据的实体的特定属性,而无需重构您的模型:

The answer to this question was accepted, but FYI, here's how to fetch only specific properties of an entity with Core Data, without re-factoring your model:

// build your NSFetchRequest
// ...

[request setResultType:NSDictionaryResultType];
[request setPropertiesToFetch:
  [NSArray arrayWithObjects:@"property1", @"property2", /* etc. */ nil]];    

另请参阅 NSFetchRequest.

这篇关于是否可以在核心数据查询中仅获取选定的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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