Search code examples
iosobjective-cnsfetchedresultscontrollernsfetchrequest

how to fetch result from core data sorted by the object creation date?


sorry if the title is not clear, i'll explain:

I have a note creation page, after creating a note I'm saving the note using the model class (nsmanagedobject sub class).

I have another table view controller which I'm showing the notes, and I want the notes on the table view to be sorted by creation date, is there a key for that?

this is what i have now:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"body" ascending:YES];

the key here is the text of the note so its sorted by the abc, can you help me to sort it by date?

tnx


Solution

  • There is no implicit creation date attribute for a managed object. You can create an attribute, say creationDate, in your model and set it to [NSDate date] when you create an object.

    Then you can simply sort on this attribute

     NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:YES];