3

I am new in CoreData and I am trying to fetch only one column Data. I am trying using below code:

//MARK :- Fetch All Calculation
func fetchUniqueParentAxis(testID : String) -> [String]  {

    var arrAxis : [String] = []

    let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "TesCalculation")
    fetchRequest.predicate = NSPredicate(format: "testID == %@ ",testID)
    fetchRequest.includesPropertyValues = true
    fetchRequest.returnsObjectsAsFaults = false
    fetchRequest.propertiesToFetch = ["parentAxis"]

    do {
         calculation = try AppDelegate.getContext().fetch(fetchRequest) as! [TesCalculation]//try AppDelegate.getContext().fetch(fetchRequest) as! [String : AnyObject]
    }

    catch let error {
        //Handle Error
        Helper.sharedInstance.Print(error as AnyObject)
    }
 }. 

"parentAxis" is my a column and I want to fetch data of that column only .

2 Answers 2

11

You can absolutely fetch a subset of columns in CoreData. Yes, I agree Core Data IS an Object Graph solution that can use SQLite as a storage engine. However, fetching a subset of data instead of an entire NSManagedObject has been possible to do for a very long time in CoreData but recent changes have made it slightly different than before.

let fetchRequest = NSFetchRequest<NSDictionary>(entityName: TesCalculation.entity().name!)
fetchRequest.resultType = .dictionaryResultType
fetchRequest.predicate = NSPredicate(format: "testID == %@ ",testID)
fetchRequest.propertiesToFetch = ["parentAxis"]
do {
    let results = try AppDelegate.getContext().fetch(fetchRequest)
} catch let error as NSError {
    print(error)
}

What this will return you is something that looks like this:

[
   {
      "parentAxis": "abc"
   },
   {
      "parentAxis": "xyz"
   }
]

The question was not about performance gain (even though there might be; I truly have no idea!) but rather if/how this can be done and this is how it can be done. Plus, I disagree with other statements made that it "doesn't make sense to have a property without an object." There are plenty of cases where you are allocating objects where all you need is a property or two for the need. This also comes in handy if you are loosely coupling Entities between multiple stores. Of course there are other methods for this as well (your xcdatamodel's Fetched Properties) so it just really depends on the use case.

Just to show that it has been around for some time this is in the header of the NSFetchRequest under NSFetchRequestResultType:

@available(iOS 3.0, *)
public static var dictionaryResultType: NSFetchRequestResultType { get }
Sign up to request clarification or add additional context in comments.

Comments

2

Core data is an object model. It translates rows of the sql database into objects with properties. You are not running queries directly on the SQL, so you cannot do everything in core-data that you could do with SQL. You interact with object which interact with the database. In core-data you can have an object that is not faulted. It means that none of its properties are loaded into memory, but when you need them (when you access a property) it will fetch it from the database. You cannot have a object that has only some of its properties set. They are either all set (ie faulted) or none are set (it is not faulted).

There really isn't much to be gained in most cased by only fetch one or two columns. The extra data transfer is often minimal. The only exception is when you have a large blob of data as as property on the object. In that case you should store the blob on a separate entity and give it a one-to-one relationship. That way the expensive block of data isn't loaded into memory until it is requested.

5 Comments

Then is there any alternative that I Can fetch by column
in core-data, a column is a property and an row is an object. It doesn't make sense to have a property without an object.
Thanks to explain me.
@kishor0011: it looks like this is a full and complete answer to your question. Would you consider accepting it, please?
stackoverflow.com/questions/50231512/… Please help me for this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.