What is the best way to make a select query in CoreData?

I am trying to find the most efficient way to query a sample in CoreData. Earlier, I first checked if an error existed, and if I had not checked the array of the returned object. Is there a faster way to do this. Is this something like an accepted way to make a request?

let personsRequest = NSFetchRequest(entityName: "Person") var fetchError : NSError? //Is it okay to do the fetch request like this? What is more efficient? if let personResult = managedObjectContext.executeFetchRequest(personRequest, error: &fetchError) as? [Person] { println("Persons found: \(personResult.count)") } else { println("Request returned no persons.") if let error = fetchError { println("Reason: \(error.localizedDescription)") } } 

Regards, Fisher

0
source share
1 answer

Validating the return value of executeFetchRequest() . The return value is nil if the selection is not performed, in which case the error variable will be set, so there is no need to check if let error = fetchError .

Note that the request does not work if there is no (corresponding) object. In this case, an empty array is returned.

 let personRequest = NSFetchRequest(entityName: "Person") var fetchError : NSError? if let personResult = managedObjectContext.executeFetchRequest(personRequest, error: &fetchError) as? [Person] { if personResult.count == 0 { println("No person found") } else { println("Persons found: \(personResult.count)") } } else { println("fetch failed: \(fetchError!.localizedDescription)") } 
+3
source

All Articles