View Full Version : Question about Core Data
Nov 14, 2010, 12:59 PM
I've been working with Core Data for a while, and one thing doesn't make much sense to me. Based on the order the data appears in the tableView or Core Data Widget, and based on what I read in the XML file that the program saves to, there doesn't seem to be any ordering to the information. This doesn't make much sense to me, as I would think it'd be better to save and retrieve information in a set order, especially with a database. Is there some way to set an order when my program is saving the objects to a file, and then retrieve them in that same order, or is it just naturally chaotic?
Nov 14, 2010, 01:23 PM
Though I don't really know the answer, here are some things to think about.
There are three commonly accepted ways of storing a binary tree in a flattened list. You could look at your XML file assuming it was originally a binary tree and try to see if the randomness you see is possibly not so random.
Apple has stated that Core Data is as fast as it can be regardless of the underlying store, so even if it isn't a binary tree, I would have to assume that is organized in some way that is efficient to Core Data itself.
Nov 14, 2010, 02:05 PM
From the Core Data programming guide:
Objects in a persistent store are unordered. Typically you should impose order at the controller or view layer, based on an attribute such as creation date. If there is order inherent in your data, you need to explicitly model that.
One of the issues at hand is what is "ordered"? Something that's ordered for one purpose (e.g. creation date) is of no help with something like a name. As such there's not really much point in saving in one order because it has no idea if it's a useful one.
You can index a certain attribute which will speed up certain operations like sorting and equality, but not others such as CONTAINS.
Are you having an issue with performance, or are you just curious? You seem to ask a lot of question regarding Core Data and the programming guide and free WWDC 2010 videos contain a lot of useful information on the topic.
Nov 14, 2010, 02:05 PM
CoreData does NOT sort your data, nor does it make any guarantees as to the order of data in a query. You have to sort the data yourself, or add an "index" NSUInteger field to all of your data objects.
"Core Data expects you to define your own sorting with NSSortDescriptors, so you'll have to add your own attribute to track a user-customizable ordering index like that."
"Core Data notoriously pays no attention to the order of objects."
Nov 14, 2010, 02:35 PM
Thank you Odie. That's not the answer I was hoping for, but it is an answer.
Josh, this question is directly related to the last core-data issue I posted on here, mainly due to the fact that we never found a solution.
Nov 15, 2010, 08:57 AM
It's quite common for lists of things in computing to be in an unpredictable order, Java collections are one. I've always though that odd, you'd think there would be SOME kind of order, even if it's not that one you expect, but that's the way it goes.
Your code should not assume it's data is going to arrive in a predictable order - what happens if someone fiddles with the data source and changes that order without you realising? Much better for your program to sort the data itself as it requires, either with an ORDER BY clause in SQL or by sorting your Core Data result set when it returns. The code is the much more resilient.
Nov 15, 2010, 09:06 AM
Oooo thats disappointing. I'll have to redeem myself in your other Core Data related thread.