Ordered Sets and Core Data (NSOrderedSet)

AlexR picture AlexR · Apr 25, 2013 · Viewed 7.8k times · Source

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!

Answer

Douglas Fischer picture Douglas Fischer · Apr 27, 2013

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.