3

Suppose a table named TBL_person has 4 columns and I want to fetch data of only id and name column

enter image description here

In SQL I can do it like this:

SELECT id, name FROM TBL_person ORDER BY id ASC

But what to do in SWIFT?

    let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
    let moContext:NSManagedObjectContext = appDel.managedObjectContext

    let fetchRequest = NSFetchRequest()

    let entityDescription = NSEntityDescription.entityForName("TBL_person", inManagedObjectContext: moContext)

    fetchRequest.entity = entityDescription

    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]

    do {
        let personList = try moContext.executeFetchRequest(fetchRequest) as! [ModelPerson]

        print("test------", personList.count)

    } catch {
        let fetchError = error as NSError
        print(fetchError)
    }
2
  • 2
    Lookup propertiesToFetch ... Commented Nov 25, 2015 at 10:47
  • Thank you @MartinR . It worked :) Commented Nov 25, 2015 at 11:23

2 Answers 2

2

Thank you @Martin R. Your comment helped me out the solution. For other people if necessary:

let entityDescription = NSEntityDescription.entityForName("TBL_person", inManagedObjectContext: moContext)

    fetchRequest.entity = entityDescription

    fetchRequest.propertiesToFetch = ["id","name"]
    fetchRequest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]

    do {
        let personList = try moContext.executeFetchRequest(fetchRequest) as! [ModelPerson]

        print("test------", personList.count)

    } catch {
        let fetchError = error as NSError
        print(fetchError)
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Actually propertiesToFetch is only considered if the return type is NSDictionaryResultType, NSManagedObject is always returned in all.
0

I found result in Core Data Programming Guide. Hope to help you. https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/FetchingObjects.html#//apple_ref/doc/uid/TP40001075-CH6-SW1

Filtering Results

The real flexibility in fetching objects comes in the complexity of the fetch request. To begin with, you can add an NSPredicate object to the fetch request to narrow the number of objects being returned. For example, if you only want Employee objects that have a firstName of Trevor, you add the predicate directly to NSFetchRequest:

OBJECTIVE-C

NSString *name = @"Trevor";
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"name == %@", name]];

SWIFT
let name = "Trevor"
fetchRequest.predicate = NSPredicate(format: "name == %@", name)

In addition to narrowing the objects being returned, you can configure how those objects are returned. For example, you can instruct Core Data to return NSDictionary instances instead of fully formed NSManagedObject instances. Further, you can configure the NSFetchRequest so that those NSDictionary instances only contain a subset of the properties available on the Employee entity.

Comments

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.