我可以通过索引访问CoreData对象吗?

时间:2023-01-27 16:00:55

I'm using CoreData as a persistent store for my app data but would like to access elements in my data model by index rather than iterating through the whole collection every time.

我正在使用CoreData作为我的应用程序数据的持久存储,但是希望通过索引访问我的数据模型中的元素,而不是每次都遍历整个集合。

For example if I have data of the form:

例如,如果我有以下形式的数据:

  1. "George"
  2. "Bill"
  3. "George"
  4. "Barack"
  5. "Donald"

I'd like to be able to update the elements without searching the collection each time.

我希望每次都能在不搜索集合的情况下更新元素。

If the data was in an array this would be easy ie:

如果数据在数组中,这将很容易,即:

Presidents[5] = "Hilary"

There doesn't seem to be an easy way to do this in CoreData other than

似乎没有一种简单的方法可以在CoreData中执行此操作

for president in Presidents{
    if(president.id == 5){
        president.name = "Hilary"
     }
}

Which for large data sets will get hugely expensive. Am I missing something?

对于大型数据集来说,这将非常昂贵。我错过了什么吗?

2 个解决方案

#1


1  

Credit to @JohnElemans but he posted a comment rather than an answer so I'm posting this for others benefit.

感谢@JohnElemans,但他发表了评论而不是答案,所以我发布这个给别人带来好处。

Loading the objects from the Managed Object Context into an array (or dictionary) passes them by reference.

将对象从托管对象上下文加载到数组(或字典)中会通过引用传递它们。

In the example above, assuming there is an object in presidents[5], it can be accessed in the conventional way.

在上面的例子中,假设在总统[5]中有一个对象,可以用传统的方式访问它。

presidents[5] = "Hilary"

#2


0  

If you are using CoreData you should retrieve data objects through fetching. This allows you to only load what is necessary. In this particular case, retrieving by CoreData's objectid has it's own method. Otherwise you would want to fetch using a predicate.

如果您使用的是CoreData,则应通过提取来检索数据对象。这允许您仅加载必要的内容。在这种特殊情况下,通过CoreData的objectid检索它有自己的方法。否则你会想要使用谓词来获取。

This link accomplishes what you are looking for (in swift): How can I get core data entity by it's objectID?

这个链接实现了你正在寻找的东西(在swift中):我如何通过它的objectID获取核心数据实体?

In Obj-c: How to get Core Data object from specific Object ID?

在Obj-c:如何从特定的对象ID获取Core Data对象?

I'm unable to find information on the implementation but as table memory sizes are fixed I think it's likely the retrieval mechanism would be similar to an array, with the added overhead of accessing disk. You do not want to manually retrieve all objects and find the objectid yourself.

我无法找到有关实现的信息但是由于表的内存大小是固定的,我认为检索机制可能类似于一个数组,并增加了访问磁盘的开销。您不想手动检索所有对象并自己找到objectid。

#1


1  

Credit to @JohnElemans but he posted a comment rather than an answer so I'm posting this for others benefit.

感谢@JohnElemans,但他发表了评论而不是答案,所以我发布这个给别人带来好处。

Loading the objects from the Managed Object Context into an array (or dictionary) passes them by reference.

将对象从托管对象上下文加载到数组(或字典)中会通过引用传递它们。

In the example above, assuming there is an object in presidents[5], it can be accessed in the conventional way.

在上面的例子中,假设在总统[5]中有一个对象,可以用传统的方式访问它。

presidents[5] = "Hilary"

#2


0  

If you are using CoreData you should retrieve data objects through fetching. This allows you to only load what is necessary. In this particular case, retrieving by CoreData's objectid has it's own method. Otherwise you would want to fetch using a predicate.

如果您使用的是CoreData,则应通过提取来检索数据对象。这允许您仅加载必要的内容。在这种特殊情况下,通过CoreData的objectid检索它有自己的方法。否则你会想要使用谓词来获取。

This link accomplishes what you are looking for (in swift): How can I get core data entity by it's objectID?

这个链接实现了你正在寻找的东西(在swift中):我如何通过它的objectID获取核心数据实体?

In Obj-c: How to get Core Data object from specific Object ID?

在Obj-c:如何从特定的对象ID获取Core Data对象?

I'm unable to find information on the implementation but as table memory sizes are fixed I think it's likely the retrieval mechanism would be similar to an array, with the added overhead of accessing disk. You do not want to manually retrieve all objects and find the objectid yourself.

我无法找到有关实现的信息但是由于表的内存大小是固定的,我认为检索机制可能类似于一个数组,并增加了访问磁盘的开销。您不想手动检索所有对象并自己找到objectid。