I have a list of share prices with the properties dateTime
and value
.
Currently I am sorting the share prices when fetching them using a sort descriptor.
Now, I would like to change my code and store them in already sorted order to faster retrieve the most current share price (dateTime = max).
I am inserting the share prices one by one using
SharePrice *priceItem= [NSEntityDescription insertNewObjectForEntityForName:@"SharePrice" inManagedObjectContext:context];
How can I sort the share prices right after inserting them using a sort descriptor on dateTime
?
Or would I need to convert it to a NSMutableOrderedSet
and use sortUsingComparator:
?
Thank you!
Mark the Ordered checkbox of your property in your CoreData model.
Also be sure to set the CoreData automatic migration in your AppDelegate (options dictionary when creating PersistentStore). This way CoreData will be able to make this change without data lost.
i would like to add to the accepted answer, if you have a NSSet
, it must be changed to a NSOrderedSet
after selecting the ordered checkbox
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With