Saving to CoreData - Add to Top
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 to开发者_开发问答p. 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
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.
Add a creationDate
in the model, and the following code to your custom implementation of the object:
- (void)awakeFromInsert {
[self setPrimitiveCreationDate:[NSDate date]];
}
精彩评论