Ordered Sets and Core Data (NSOrderedSet)

≡放荡痞女 提交于 2019-12-03 13:44:46

问题


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!


回答1:


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.




回答2:


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




回答3:


I'm not sure how using an NSMutableOrderedSet would affect the underlying sqlite 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.



来源:https://stackoverflow.com/questions/16217925/ordered-sets-and-core-data-nsorderedset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!