Implementing Transient Properties

后端 未结 1 1145
不思量自难忘°
不思量自难忘° 2020-12-29 11:22

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

相关标签:
1条回答
  • 2020-12-29 11:41

    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 讨论(0)
提交回复
热议问题