Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 343
Bill Smithed Avatar asked Aug 24 '10 03:08

Bill Smithed


1 Answers

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.

like image 149
Marcus S. Zarra Avatar answered Oct 21 '22 04:10

Marcus S. Zarra