Swift Core Data - Query with Great Results

how can i call es request with different values ​​in swift?

This is my code:

let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let context: NSManagedObjectContext = appDelegate.managedObjectContext

    let request = NSFetchRequest(entityName: "MedicalData")
    request.propertiesToFetch = NSArray(object: "docID")
    request.returnsObjectsAsFaults = false
    request.returnsDistinctResults = true

    var results:NSArray = context.executeFetchRequest(request, error: nil)

    for data in results {
        var thisData = data as MedicalData
        println(thisData.docID)
    }

I want to get different values ​​for "docID", but I get all entities: (

Thanks for the help!

+4
source share
3 answers

You need to install

request.resultType = NSFetchRequestResultType.DictionaryResultType

It returns dictionaries, but a separate filter should work.

If you do not want to follow this route, filter in memory (also recommended). Make a normal selection and then

let distinct = NSSet(array: results.valueForKeyPath("docID") as [String])

With Swift 2.0, I prefer

let distinct = NSSet(array: results.map { $0.docID })
+6
source

As already mentioned, the key uses

fetchRequest.resultType = NSFetchRequestResultType.DictionaryResultType

and

fetchRequest.propertiesToFetch = ["propertyName"]

both are required to do the job

fetchRequest.returnsDistinctResults = true

Swift Dictionaries .

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
    let managedContext = appDelegate.managedObjectContext!
    //FetchRequest
    let fetchRequest = NSFetchRequest(entityName: "Purchase")
    fetchRequest.propertiesToFetch = ["year"]
    fetchRequest.resultType = NSFetchRequestResultType.DictionaryResultType
    fetchRequest.returnsDistinctResults = true
    //Fetch
    var error: NSError?
    if let results = managedContext.executeFetchRequest(fetchRequest, error: &error)  {
        var stringResultsArray: [String] = []
        for var i = 0; i < results.count; i++ {
            if let dic = (results[i] as? [String : String]){
                if let yearString = dic["year"]?{
                    stringResultsArray.append(yearString)
                }
            }
        }
        return stringResultsArray
    }else {
        println("fetch failed: \(error?.localizedDescription)")
    }
    return []
+4

NSPredicate , :

  var StoredResults = [NSManagedObject]()

  func fetchRequest(docID : String ){

        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        let managedContext = appDelegate.managedObjectContext
        let request = NSFetchRequest(entityName: "EntityContainingDocID")

        let predicate =  NSPredicate(format:"docID == %@", docID)
        request.predicate = predicate

    do {
        let results =
        try managedContext.executeFetchRequest(request)

        StoredResults = results as! [NSManagedObject]

        } catch let error as NSError {
              print(" error executing fetchrequest  ", error)
        }
    }

In this example, we only want to return certain values ​​that correspond to the row in the "docID" column, but NSPredicate is a convenient way to create SQL-like queries in your code and is quite flexible.

-2
source

Source: https://habr.com/ru/post/1546150/


All Articles