核心数据,当NSFetchRequest返回NSDictionaryResultType时如何获取NSManagedObject的ObjectId? [英] Core data, how to get NSManagedObject's ObjectId when NSFetchRequest returns NSDictionaryResultType?

查看:258
本文介绍了核心数据,当NSFetchRequest返回NSDictionaryResultType时如何获取NSManagedObject的ObjectId?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSFetchRequest ,它返回 NSDictionaryResultType 中的对象属性。是否有可能在这个字典中获取对象的ObjectId?否则,我需要运行返回类型为 NSManagedObjectResultType 的查询,这对于大量返回的项目来说要慢得多。

I have an NSFetchRequest which is returning the objects' properties in an NSDictionaryResultType. Is it possible to also get the objects' ObjectId within this dictionary? Otherwise I will need to run the query with a return type of NSManagedObjectResultType which is much slower for a large number of returned items.

推荐答案

是的,你可以使用非常漂亮但不完整的 NSExpressionDescription 类。您需要将正确配置的 NSExpressionDescription 对象添加到您通过设置的 NSPropertyDescription 对象的数组>

Yes you can, using the very nifty but badly-documented NSExpressionDescription class. You need to add a properly-configured NSExpressionDescription object to the array of NSPropertyDescription objects you set via setPropertiesToFetch: for your NSFetchRequest.

为您的 NSFetchRequest 指定

NSExpressionDescription* objectIdDesc = [[NSExpressionDescription new] autorelease];
objectIdDesc.name = @"objectID";
objectIdDesc.expression = [NSExpression expressionForEvaluatedObject];
objectIdDesc.expressionResultType = NSObjectIDAttributeType;

myFetchRequest.propertiesToFetch = [NSArray arrayWithObjects:objectIdDesc, anotherPropertyDesc, yetAnotherPropertyDesc, nil];
NSArray* fetchResults = [myContext executeFetchRequest:myFetchRequest error:&fetchError];

然后你应该有一个 @objectID键入您从您的抓取请求中获取的字典。

You should then have a @"objectID" key in the the dictionaries you get back from your fetch request.

这篇关于核心数据,当NSFetchRequest返回NSDictionaryResultType时如何获取NSManagedObject的ObjectId?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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