Skalwalker
Skalwalker

Reputation: 309

Request just one attribute of a entity

So I am using one core data file with a single one entity named BookArray, inside that entity I have four different attributes, what I want to do is to request just one of those attributes from the entity not all. Is it possible?

    var appDel: AppDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
    var context:NSManagedObjectContext = appDel.managedObjectContext!
    var request = NSFetchRequest(entityName: "BookArray")
    request.returnsObjectsAsFaults = false
    bookArray = context.executeFetchRequest(request, error: nil)!

Suppose I have an attribute called sciFi and another named drama, how would I request just the drama attribute?

Upvotes: 1

Views: 59

Answers (1)

pbasdf
pbasdf

Reputation: 21536

You can, by adding:

request.propertiesToFetch = ["drama"]
request.resultType = .DictionaryResultType

but, unless your other properties are big, it's unlikely to be worthwhile: your bookArray will then contain an array of dictionaries, from which you will need to unpack the relevant values: you might just as well do that directly from the array of NSManagedObjects returned by a normal fetch.

Upvotes: 1

Related Questions