Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...