核心数据可转换属性不适用于 NSPredicate
我经常将 Transformable
用于 Core Data 属性
,因此我可以稍后更改它们。
但是,似乎如果我想使用 NSPredicate
查找 NSManagedObject
,请使用 "uniqueKey == %@"
或 “uniqueKey MATCHES[cd] %@”
,它没有按预期工作。
它总是会错过匹配的对象,直到我将匹配对象的 uniqueKey 的属性更改为具有特定的类,例如 NSString
或 NSNumber
。
有人可以解释使用带有 Transformable
属性的 NSPredicate
的限制吗?
I often use Transformable
for Core Data attributes
, so I can change them later.
However, it seems like, if I want to use NSPredicate
to find a NSManagedObject
, using "uniqueKey == %@"
, or "uniqueKey MATCHES[cd] %@"
, it's not working as it should.
It always misses matching objects, until I change the attributes of the uniqueKey of the matching object to have specific class like NSString
, or NSNumber
.
Can someone explain the limitation of using NSPredicate
with Transformable
attributes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
注意:我不确定自 5/2011 以来何时/是否发生了变化(来自 Scott Ahten 接受的答案),但您绝对可以使用 NSPredicate 对可转换属性进行搜索。 Scott 正确地解释了为什么你的假设被打破,但是如果你的问题是有人能解释使用带有 Transformable 属性的 NSPredicate 的限制吗?,他暗示这是不可能的,这是不正确的。
由于这是“核心数据可转换值搜索nspredicate”(我搜索试图寻找灵感的内容)的第一个谷歌搜索,我想添加我的工作答案。
如何使用具有可转换属性的 NSPredicate
简短而令人兴奋的答案:您需要对数据转换器保持聪明。您需要将值转换为 NSData,其中包含我所说的“原始识别信息”,即可用于重建对象的最小、最具识别性的字节集。长答案,...
最重要的是,考虑:
现在,除了这些考虑之外,可变形属性相当灵活。坦率地说,为 Foo 实例编写一个 NSValueTransformer“FooToData” 到 NSData 似乎比编写大量临时自定义代码更干净。我还没有发现 Core Data 不知道它需要使用注册的 NSValueTransformer 来转换数据的情况。
继续简单地解决这些问题:
NSString *name = @"FooTrans"; [NSValueTransformer setValueTransformer:[NSClassFromString(name) new] forName:name];
您可能不想使用大量查询数据操作的转换 - 例如,主键信息使用变压器的大型导入 - 哎呀!
最后,我只是用它来测试具有 NSPredicates 的模型上高级对象属性的相等性——例如“%K == %@”——并且它工作得很好。我还没有尝试过一些不同的匹配术语,但如果它们有时有效而有时无效,我不会感到惊讶。
这是 NSURL 到 NSData 转换器的示例。为什么不只存储字符串呢?是的,这很好——这是自定义代码屏蔽存储属性的一个很好的例子。这个例子说明了一个额外的字节被添加到字符串化的 URL 中来记录它是否是一个文件 URL——让我们知道在解包对象时要使用什么构造函数。
...
Note: I'm not sure when/if this has changed since 5/2011 (from Scott Ahten's accepted answer), but you can absolutely search with NSPredicate on transformable attributes. Scott correctly explained why your assumptions were broken, but if Can someone explain the limitation of using NSPredicate with Transformable attributes? was your question, he implied that it is not possible, and that is incorrect.
Since the is the first google hit for "Core Data transformable value search nspredicate" (what I searched for trying to find inspiration), I wanted to add my working answer.
How to use NSPredicate with transformable properties
Short, heady answer: you need to be smart about your data transformers. You need to transfrom the value to NSData that contains what I'll call "primitive identifying information", i.e. the smallest, most identifying set of bytes that can be used to reconstruct your object. Long answer, ...
Foremost, consider:
Now, past those considerations, transformable attributes are rather slick. Frankly, writing an NSValueTransformer "FooToData" for Foo instances to NSData seemed cleaner than writing a lot of adhoc custom code. I haven't found a case where Core Data doesn't know it needs to transform the data using the registered NSValueTransformer.
To proceed simply address these concerns:
NSString *name = @"FooTrans"; [NSValueTransformer setValueTransformer:[NSClassFromString(name) new] forName:name];
You probably don't want to use transforms heavily queried data operations - e.g. a large import where the primary key information uses transformers - yikes!
And then in the end, I simply use this to test for equality for high-level object attributes on models with NSPredicates -- e.g. "%K == %@" -- and it works fine. I haven't tried some of the various matching terms, but I wouldn't be surprised if they worked sometimes, and others not.
Here's an example of an NSURL to NSData transformer. Why not just store the string? Yeah, that's fine -- that's a good example of custom code masking the stored attribute. This example illustrates that an extra byte is added to the stringified URL to record if it was a file URL or not -- allowing us to know what constructors to use when the object is unpacked.
...
可转换属性通常作为存档的二进制数据保存。因此,您尝试将 NSData 的实例与 NSString 或 NSNumber 的实例进行比较。
由于这些类以不同的方式解释相同的数据,因此它们不被视为匹配。
Transformable attributes are usually persisted as archived binary data. As such, you are attempting to compare an instance of NSData with an instance of NSString or NSNumber.
Since these classes interpret the same data in different ways, they are not considered a match.
你可以试试这个方法
you can try this way