Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
291 views
in Technique[技术] by (71.8m points)

iphone - Is it possible to fetch only selected property in a Core Data query

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

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.

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?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]];    

See also the documentation for NSFetchRequest.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...