Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordered Sets and Core Data (NSOrderedSet)

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!

like image 614
AlexR Avatar asked Apr 25 '13 14:04

AlexR


2 Answers

Ordered property on CoreData

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.

like image 52
Douglas Fischer Avatar answered Nov 02 '22 13:11

Douglas Fischer


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

like image 3
Neuro Avatar answered Nov 02 '22 11:11

Neuro