Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question

1 Answer

I'm not sure how using an NSMutableOrderedSet would affect the underlying mysql database. It is nice to think that it would store it in the order of the set, but the underlying implementation is a blackbox and I don't think there's any guarantee that it would work consistently, if it worked at all.

If it's a performance gain you are looking for, I would think adding an index to that attribute would be the best approach.

share|improve this answer
With that said, mysql does support clustered indexes (which affects the sort on disk). I am not sure if it is exposed in CoreData, but that is something you could look into. – Joseph DeCarlo 26 mins ago
I have the attribute indexed and do the sort with each fetch. However, I need to improve the performance and would like to store the items in sorted order. – AlexR 21 mins ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.