有序集和核心数据(NSOrderedSet)

时间:2022-02-23 18:06:44

I have a list of share prices with the properties dateTime and value.

我有一个股票价格列表,其中包含dateTime和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).

现在,我想更改我的代码并将其存储在已排序的顺序中,以便更快地检索最新的股价(dateTime = max)。

I am inserting the share prices one by one using

我正在使用逐个插入股价

SharePrice *priceItem= [NSEntityDescription insertNewObjectForEntityForName:@"SharePrice" inManagedObjectContext:context];

SharePrice * priceItem = [NSEntityDescription insertNewObjectForEntityForName:@“SharePrice”inManagedObjectContext:context];

How can I sort the share prices right after inserting them using a sort descriptor on dateTime?

如何在dateTime上使用排序描述符插入股票价格后立即对其进行排序?

Or would I need to convert it to a NSMutableOrderedSet and use sortUsingComparator:?

或者我需要将其转换为NSMutableOrderedSet并使用sortUsingComparator:?

Thank you!

3 个解决方案

#1


21  

有序集和核心数据(NSOrderedSet)

Mark the Ordered checkbox of your property in your CoreData model.

在CoreData模型中标记属性的Ordered复选框。

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.

还要确保在AppDelegate中设置CoreData自动迁移(创建PersistentStore时选项字典)。这样,CoreData将能够在不丢失数据的情况下进行此更改。

#2


3  

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

我想添加到接受的答案,如果你有一个NSSet,它必须在选择有序的复选框后更改为NSOrderedSet

#3


0  

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.

我不确定使用NSMutableOrderedSet会如何影响底层的sqlite数据库。很高兴认为它会按照集合的顺序存储它,但底层实现是一个黑盒子,我认为没有任何保证它会一致地工作,如果它工作的话。

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

如果它是您正在寻找的性能增益,我认为为该属性添加索引将是最好的方法。

#1


21  

有序集和核心数据(NSOrderedSet)

Mark the Ordered checkbox of your property in your CoreData model.

在CoreData模型中标记属性的Ordered复选框。

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.

还要确保在AppDelegate中设置CoreData自动迁移(创建PersistentStore时选项字典)。这样,CoreData将能够在不丢失数据的情况下进行此更改。

#2


3  

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

我想添加到接受的答案,如果你有一个NSSet,它必须在选择有序的复选框后更改为NSOrderedSet

#3


0  

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.

我不确定使用NSMutableOrderedSet会如何影响底层的sqlite数据库。很高兴认为它会按照集合的顺序存储它,但底层实现是一个黑盒子,我认为没有任何保证它会一致地工作,如果它工作的话。

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

如果它是您正在寻找的性能增益,我认为为该属性添加索引将是最好的方法。