Question about Core Data

Discussion in 'Mac Programming' started by BadWolf13, Nov 14, 2010.

  1. macrumors 6502

    Joined:
    Dec 17, 2009
    #1
    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?
     
  2. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #2
    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.
     
  3. macrumors regular

    Joined:
    Apr 8, 2009
    #3
    From the Core Data programming guide:

    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.
     
  4. macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #4
    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.

     
  5. thread starter macrumors 6502

    Joined:
    Dec 17, 2009
    #5
    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.
     
  6. macrumors 6502a

    Joined:
    Feb 18, 2008
    #6
    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.
     
  7. macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #7
    Oooo thats disappointing. I'll have to redeem myself in your other Core Data related thread.
     

Share This Page