Reputation: 1581
I've been implementing the core data framework lately and every step of the implementation had a hidden issue behind.
Here comes the issue i'm not really sure how to solve.
So far i've implemented a save and fetch functions for my tableview.
First of all fetch was returning data faults, so i've implemented
fetchRequest.returnsObjectsAsFaults = false
now the data was fed and shown in the tableview, but it had a massive amount of duplicates and was in chaotic order, so i've added
fetchRequest.returnsDistinctResults = true
but this didn't work so after googling i've implemented the following one liner
fetchRequest.resultType = NSFetchRequestResultType.DictionaryResultType
as a final result the fetch function doesn't run at all. debugger simply skips the action and the app returns an empty tableview.
Here is my code. Looking forward to any suggestions:
func fetchItems() {
// get the managed object context
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedObjectContext = appDelegate.managedObjectContext!
// create fetch requests
let fetchRequest = NSFetchRequest(entityName:"Entities")
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.returnsDistinctResults = true
fetchRequest.resultType = NSFetchRequestResultType.DictionaryResultType
var fetchedResults : [NSManagedObject]?
do {
fetchedResults = try managedObjectContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]
}
catch {
print("Could not fetch \(error)")
}
if let results = fetchedResults {
someOtherArray = [results]
}
}
Upvotes: 0
Views: 364
Reputation: 21536
I doubt very much that you want or need these lines:
fetchRequest.returnsObjectsAsFaults = false
fetchRequest.returnsDistinctResults = true
fetchRequest.resultType = NSFetchRequestResultType.DictionaryResultType
The first is primarily a performance optimisation. "Faults" are generally speaking not a cause for concern - CoreData is saving memory by not populating the property values for all the objects returned by the fetch. But it will do so automatically for each object as soon as you try to access any of its properties. So that line is fine, but unnecessary.
Regarding the duplicate data, you shouldn't be using returnsDistinctResults
to suppress them, you should avoid creating them.
The final line is the cause of your empty table view. Because you specify DictionaryResultType
, the array returned by the fetch is an array of dictionaries. But with this line:
fetchedResults = try managedObjectContext.executeFetchRequest(fetchRequest) as? [NSManagedObject]
you are then trying to cast that array as an array of NSManagedObjects. That cast fails, and consequently fetchedResults is nil, and consequently someOtherArray
is not set.
Regarding the chaotic order, you should use a sort descriptor (see the NSSortDescriptor documentation) to sort the results how you would like.
Finally, as in your other question, you are setting someOtherArray
to be [results], which is an array containing the results
array. I suspect you actually want:
someOtherArray = results
Upvotes: 1