如何获取对象ID,但还包括CoreData中的行数据? [英] How to fetch just object IDs but also include the row data in CoreData?

查看:100
本文介绍了如何获取对象ID,但还包括CoreData中的行数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CoreData苹果文档在后台工作,我遇到了这一点的建议:

In the CoreData Apple Docs on working in the background, I came across this bit of advice:


例如,您可以将抓取请求配置为仅返回对象ID ,但也包括行数据(并更新行缓存) - 如果您要传递那些对象ID从后台线程到另一个线程。

For example, you can configure a fetch request to return just object IDs but also include the row data (and update the row cache)—this can be useful if you're just going to pass those object IDs from a background thread to another thread.

我想知道如何实现这个获取请求?具体如何更新行缓存。

I was wondering how you might implement this fetch request? Specifically how to update the row cache.

我这是如何获取ID:

I think this is how get just the IDs:

NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"MyItem"]; // iOS 5 method
request.returnsObjectsAsFaults = YES; 

如何处理?

推荐答案

默认情况下,includesPropertyValues为YES,returnsObjectsAsFaults也为YES。

By default, includesPropertyValues is YES, and returnsObjectsAsFaults is also YES.

如果你只想返回对象ID,使用...

If you just want to return object IDs, you need to use...

fetchRequest.resultType = NSManagedObjectIDResultType;

但是,没有要提取的属性,行缓存不会被填充。

However, there are no properties to be fetched, and the row cache will not be populated. You will just get back a bunch of object IDs.

注意,BY DEFAULT,(resultType == NSManagedObjectResultType),因为includesPropertyValues和returnsObjectsAsFaults都为YES,返回对象作为错误(对象ID是可访问的),行缓存将被填充 - 但是数据将不会真正在内存,因为对象仍然是一个错误...但你仍然可以得到其对象ID 。

Note, that BY DEFAULT, (resultType == NSManagedObjectResultType), since both includesPropertyValues and returnsObjectsAsFaults are YES, a fetch will return objects as faults (the object ID is accessible), and the row cache will be filled - but the data will not truly be "in memory" because the object is still a fault... but you can still get at its object ID.

所有你需要做的是询问对象的objectID。

All you need to do is then ask the object for its objectID.

,说你要求的行为是你默认得到的。

All that, I guess, to say that the behavior you are asking for is what you get by default. So, are you having a problem, or is there some reason you think you are NOT getting that behavior?

EDIT

Ummm ...我说的是BY DEFAULT,你得到你想要的。如果你只是填写一个获取请求,并且不改变任何属性,returnsObjectsAsFaults是YES,因此返回给你的对象将是错误。此外,includesPropertyValues也是YES - 所以一些数量的属性数据将在行缓存中可用。

Ummm... I am saying that BY DEFAULT, you are getting what you want. If you just fill out a fetch request, and don't change any of the attributes, returnsObjectsAsFaults is YES -- so objects that are returned to you will be faults. Furthermore, includesPropertyValues is also YES -- so some amount of property data will be available in the row cache.

您可以通过调用访问objectID属性,well,managedObject。 objectID。

You can access the objectID property by calling, well, managedObject.objectID.

EDIT

我很抱歉,因为我显然没有做非常好的沟通,因为这将是第三次我说过同样的事情。

I apologize, because I am obviously not doing a very good job of communicating because this will be the third time I've said the same thing.

当你创建一个NSFetchRequest,它有几个属性设置默认。

When you create a NSFetchRequest, it has several properties that are set by default.

默认情况下,resultType设置为NSManagedObjectResultType。这意味着,除非你改变它,它仍然将是NSManagedObjectResultType。这也意味着你从一个抓取获得的结果数组将包含一个NSManagedObjects数组(而不是返回一个计数,或一个字典或ObjectIDs)。

resultType is set to NSManagedObjectResultType by default. That means, unless you change it, it will still be NSManagedObjectResultType. It also means that the array of results that you get from a fetch will contain an array of NSManagedObjects (as opposed to getting back a count, or a dictionary, or ObjectIDs).

returnsObjectsAsFaults在默认情况下设置为YES。这意味着,除非你改变它,它仍然会是YES。这也意味着从获取返回的对象将是故障。 NSManagedObject不会用属性数据实现。对象将是一个故障。它将有足够的元数据来了解其类型,对象ID和其他一些东西。

returnsObjectsAsFaults is set to YES by default. That means, unless you change it, it will still be YES. It also means that objects that get returned from a fetch will be faults. The NSManagedObject will not be realized with property data. The object will be a fault. It will have enough metadata to know its type, object ID, and some other stuff.

includesPropertyValues默认设置为YES。这意味着,除非你改变它,它仍然会是YES。

includesPropertyValues is set to YES by default. That means, unless you change it, it will still be YES. It also means that some amount of property data will be fetched into the row cache.

每次从抓取返回的NSManagedObject都会:

Each NSManagedObject that gets returned from the fetch will:


  1. 拥有对象ID

  2. 成为故障,因此数据实际上并未完全加载到内存中

  3. 一些数量的属性数据将在行高速缓存中

这是您要求的一切。我不知道我可以添加什么(不再重复自己)。

This is everything you are asking for. I'm not sure what else I can add (without repeating myself yet again).

另外,注意,如果你只需要对象ID,你可以设置resultType为NSManagedObjectIDResultType。

Also, note that if you just want object IDs you can set resultType to NSManagedObjectIDResultType.

直接从NSFetchRequest文件...

Directly from the NSFetchRequest documentation...


includesPropertyValues

您可以将includPropertyValues设置为NO,以减少内存开销
,避免创建对象以表示属性值。你
通常应该这样做,但是,如果你确定你
将不需要实际的属性数据,或者你已经在行缓存中的
信息,否则你会招致多次跳过
到数据库。

You can set includesPropertyValues to NO to reduce memory overhead by avoiding creation of objects to represent the property values. You should typically only do so, however, if you are sure that either you will not need the actual property data or you already have the information in the row cache, otherwise you will incur multiple trips to the database.

在正常提取期间(includesPropertyValues为YES),Core Data
提取对象ID和属性数据匹配记录,
用信息填充行高速缓存,并返回受管对象
作为故障(请参阅returnsObjectsAsFaults)。这些故障是由
对象管理的,但是它们的所有属性数据仍然驻留在行高速缓存
中,直到故障被触发。当故障被触发时,Core Data从行缓存中检索
的数据 - 不需要回到
数据库。

During a normal fetch (includesPropertyValues is YES), Core Data fetches the object ID and property data for the matching records, fills the row cache with the information, and returns managed object as faults (see returnsObjectsAsFaults). These faults are managed objects, but all of their property data still resides in the row cache until the fault is fired. When the fault is fired, Core Data retrieves the data from the row cache—there is no need to go back to the database.

如果includesPropertyValues为NO,那么Core Data仅获取匹配记录的
对象ID信息 - 它不填充行缓存。 Core Data仍然返回受管对象,因为只有
需要受管对象ID来创建故障。然而,如果你
后来触发故障,Core Data会查找(空)行缓存,
没有找到任何数据,然后第二次返回商店
数据。

If includesPropertyValues is NO, then Core Data fetches only the object ID information for the matching records—it does not populate the row cache. Core Data still returns managed objects since it only needs managed object IDs to create faults. However, if you subsequently fire the fault, Core Data looks in the (empty) row cache, doesn't find any data, and then goes back to the store a second time for the data.

和...


returnsObjectsAsFaults

默认值为YES。如果结果类型
(请参阅resultType)是NSManagedObjectIDResultType,则不使用此设置,因为对象ID不是
具有属性值。您可以将returnsObjectsAsFaults设置为NO,以获得
a的性能优势,如果您知道您将需要从返回的对象访问属性
值。

The default value is YES. This setting is not used if the result type (see resultType) is NSManagedObjectIDResultType, as object IDs do not have property values. You can set returnsObjectsAsFaults to NO to gain a performance benefit if you know you will need to access the property values from the returned objects.

默认情况下,当你执行fetch returnsObjectsAsFaults是YES;
Core Data获取匹配记录的对象数据,用信息填充
行高速缓存,并返回托管对象作为故障。
这些故障是托管对象,但是它们的所有属性数据
都驻留在行高速缓存中,直到故障被触发。当故障是
触发时,Core Data从行高速缓存中检索数据。虽然这个操作的
开销很小,但对于大型数据集,它可能变成
非平凡。如果你需要从
返回的对象访问属性值(例如,如果你遍历所有的对象到
计算特定属性的平均值),那么它是
更多有效地将returnsObjectsAsFaults设置为NO,以避免
的额外开销。

By default, when you execute a fetch returnsObjectsAsFaults is YES; Core Data fetches the object data for the matching records, fills the row cache with the information, and returns managed object as faults. These faults are managed objects, but all of their property data resides in the row cache until the fault is fired. When the fault is fired, Core Data retrieves the data from the row cache. Although the overhead for this operation is small, for large datasets it may become non-trivial. If you need to access the property values from the returned objects (for example, if you iterate over all the objects to calculate the average value of a particular attribute), then it is more efficient to set returnsObjectsAsFaults to NO to avoid the additional overhead.

这篇关于如何获取对象ID,但还包括CoreData中的行数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆