0

All,

Is it possible to save to the top of the CoreData? If so, how?

For example, every time something is added to the data store, I want it to be inserted at the top. This way, when the results are fetched they would come back sorted by most recent first without having to save the NSDate and fetch with a predicate. Here is a crude example:

Most recent
Earlier
Yesterday
Last Week

Thanks, James

2 Answers 2

4

What is the "top"?

Core Data does not assign any particular order to the objects it stores. If you want to impose some order on the objects, add an attribute to the entity that you want to be ordered and then sort on that attribute when you fetch the objects. So, you could add a serialNumber attribute that always increases. Sorting on that serial number would order the objects.

4
  • By top I mean think visually of a list of items. The object at the top of that list.
    – Baub
    Sep 21, 2011 at 20:27
  • Or a timestamp might be more appropriate, given the examples of "Yesterday" and "Last week". But it's the same idea. Sep 21, 2011 at 20:28
  • "top" only means something if you were storing the data in something like a stack - CoreData is not a stack, it has no inherent order
    – bshirley
    Sep 21, 2011 at 20:37
  • @James, the point is that you need to define the order -- Core Data doesn't provide it for you.
    – Caleb
    Sep 21, 2011 at 20:39
0

Add a creationDate in the model, and the following code to your custom implementation of the object:

- (void)awakeFromInsert {
  [self setPrimitiveCreationDate:[NSDate date]];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.