本文介绍了实现瞬态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在基于Core Data的应用程序中添加了一个临时属性,我想我缺少了一些东西。在数据模型编辑器中,我添加了可选 transient BOOL 属性 isUnderwater



在我的模型头文件中,我添加: @property(nonatomic)BOOL isUnderwater; ,然后在实现文件中实现这些方法:

  - (BOOL)isUnderwater {
... implementation ...
return(ret);
}
- (void)setIsUnderwater:(BOOL)isUnderwater {}

但是当我尝试使用 isUnderwater 作为一个条件在NSPredicate,我得到这个错误: ***终止应用程序由于未捕获异常'NSInvalidArgumentException' ,原因:'keypath isUnderwater在实体中找不到



任何想法?



谢谢!

解决方案

首先, code> NSFetchRequest 这是违反SQLite存储。当你使用SQLite时, NSFetchRequest 被翻译成sql并在数据库中运行,很久之前你的瞬态被触动。



此外,您不应该实现访问器,应改用 @synthesize



下一页,如果你想设置transient属性,那么你应该在 -awakeFromFetch 和/或 -awakeFromInsert

接下来,你的属性应该被调用 underwater @property 定义应该是:

  @property(nonatomic,retain,getter = isUnderwater)NSNumber *水下; 

注意:即使你在你的模型中声明一个布尔值,它仍然是一个 NSNumber 在代码中。



最后,在transient属性上设置可选标志没有值,因为它将被抛弃。



更新



一旦实体位于内存中,您可以应用其他过滤器唯一的限制是,当你出去到SQLite文件时,你不能使用瞬态属性。



例如,您可以执行在所有实体中加载的 NSFetchRequest 。然后,您可以立即对返回的 NSArray 应用第二个 NSPredicate ,并进一步过滤对象。


I am adding a transient property to my Core Data-based app, and I think I am missing something. In the Data Model Editor, I added a optional, transient, BOOL property called isUnderwater.

In my model's header file, I added: @property (nonatomic) BOOL isUnderwater;, then I implemented these methods in the implementation file:

- (BOOL)isUnderwater {
    ... implementation ...
    return (ret);
}
- (void)setIsUnderwater:(BOOL)isUnderwater {}

But when I try to use isUnderwater as a condition in a NSPredicate, I get this error: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath isUnderwater not found in entity <NSSQLEntity Wheel id=2>'.

Any ideas?

Thanks!

解决方案

First, you can't use a transient property in a NSFetchRequest that is going against a SQLite store. When you are using SQLite the NSFetchRequest is translated into sql and run against the database, long before your transient is ever touched.

Also, you should not be implementing the accessors, you should be using @synthesize instead.

Next, if you are wanting to set the transient property then you should be setting it in the -awakeFromFetch and/or -awakeFromInsert instead of overriding the getter.

Next, your property should be called underwater and the @property definition should be:

@property (nonatomic, retain, getter=isUnderwater) NSNumber *underwater;

Note: even though you are declaring it a boolean in your model, it is still a NSNumber in code.

Lastly, setting the optional flag on a transient property has no value since it will be thrown away anyway.

Update

You can apply additional filters (even against transient properties) once the entities are in memory. The only limitation is that you can't use transient properties when you are going out to the SQLite file.

For example, you could perform a NSFetchRequest that loads in all of the entities. You could then immediately apply a second NSPredicate against the returned NSArray and further filter the objects down.

这篇关于实现瞬态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 14:52