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
NSFetchRequestthat is going against a SQLite store. When you are using SQLite theNSFetchRequestis 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
@synthesizeinstead.Next, if you are wanting to set the transient property then you should be setting it in the
-awakeFromFetchand/or-awakeFromInsertinstead of overriding the getter.Next, your property should be called
underwaterand the@propertydefinition should be:Note: even though you are declaring it a boolean in your model, it is still a
NSNumberin 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
NSFetchRequestthat loads in all of the entities. You could then immediately apply a secondNSPredicateagainst the returnedNSArrayand further filter the objects down.