Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Have a look at this code I wrote recently, to cache an image in an NSManagedObject:</p> <p>First you define a <strong>transient property</strong> in your model (notice that if your transient property points to an object type other than those supported by CoreData you'll leave as "<code>Undefined</code>" in the model)</p> <p><img src="https://i.stack.imgur.com/GcW1v.png" alt="enter image description here"></p> <p>Then, you re-generate your NSManagedObject subclass for that entity or just add the new property manually, the header file should look like this:</p> <pre><code>@interface Card : NSManagedObject @property (nonatomic, retain) NSString * imagePath; @property (nonatomic, retain) NSString * name; @property (nonatomic, retain) NSNumber * order; @property (nonatomic, retain) NSString * displayName; @property (nonatomic, retain) UIImage *displayImage; @end </code></pre> <p>Here we change the class of the transient property to the actual class type </p> <p>e.g. <code>displayImage</code> type here is <code>UIImage</code>.</p> <p>In the implementation file (or an extension class) you implement the getter/setter for your transient property:</p> <pre><code>-(UIImage*)displayImage{ //Get Value [self willAccessValueForKey:@"displayImage"]; UIImage *img = (UIImage*)[self primitiveValueForKey:@"displayImage"]; [self didAccessValueForKey:@"displayImage"]; if (img == nil) { if ([self imagePath]) { //That is a non-transient property on the object img = [UIImage imageWithContentsOfFile:self.imagePath]; //Set Value [self setPrimitiveValue:img forKey:@"displayImage"]; } } return img; } </code></pre> <p>Hope that helps you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload