开发者

Implementing Transient Properties

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜