J-Q
J-Q

Reputation: 372

Non-Object Attribute in Core Data, transient properties

Feel lost after reading this section: A Non-Object Attribute

According to the Basic-Approach also contained in above link, I should have 2 attributes in my custom-code when handling "transient properties":

1st attribute, for the actually-wanted (un-supported) custom type => transient attribute

2nd attribute, for shadow-representation (concrete supported) type => persistent attribute

......

My reading was very enjoyable, until reached "A Non-Object Attribute" section, which puzzle me deeply, as quoted below:

...When you implement the entity’s custom class, you typically add an instance variable for the attribute. ... 《 OK, I can follow this...make an iVar is no big deal》

If you use an instance variable to hold an attribute, you must also implement primitive get and set accessors 《 OK, I know how to do primitive-accessor. why need them? because internal-optimized-storage inside MO can be efficiently used, I guess.》

@interface MyManagedObject : NSManagedObject
  {
     NSRect myBounds; // I assume this suppose to be the **transient attribute**
  }
     @property (nonatomic, assign) NSRect bounds; // I assume this is the **persistent attribute**
     @property (nonatomic, assign) NSRect primitiveBounds; // because complier forces me to implement below primitive-accessors ?
@end   

- (NSRect)primitiveBounds
{
    return myBounds; // accessing iVAR storage for **transient attribute**? I hope so
}
- (void)setPrimitiveBounds:(NSRect)aRect
    myBounds = aRect; // accessing iVAR storage for **transient attribute**? I hope so
}

From here down below, I have... too many ???????????? unsolved

- (NSRect)bounds
  {
      [self willAccessValueForKey:@"bounds"]; //KVO notice of access **persistent attribute**, I guess
      NSRect aRect = bounds;                  //will this invoke primitive-Getter ???
      [self didAccessValueForKey:@"bounds"];
      if (aRect.size.width == 0)              //bounds has not yet been unarchived, Apple explained
      {

          NSString *boundsAsString = [self boundsAsString]; // unarchiving pseudo method, I guess
          if (boundsAsString != nil) //if that value is not nil, transform it into the appropriate type and cache it...Apple explained.
          {
              bounds = NSRectFromString(boundsAsString); //will this invoke primitive-Setter???
          }
       }
      return bounds;
  }

I put my final question list here:

1, do I STILL need to have 2 attributes to handle NON-Object-Attribute, transient attribute and persistent attribute?

2, how can iVar "myBounds" be represented/connected with "@property bounds"? Is this "@property bounds" the modeled-property in a MOM?

3, what is the purpose of implementation of primitive-accessor here? for enforcing me write KVO (will...did...) methods pair? for transferring values (in and out) between iVar "myBounds"and "@property bounds"?

4, in this line of code

 bounds = NSRectFromString(boundsAsString); //will this invoke primitive-Setter???

is primitive-Setter called OR public/standard-Setter gets called? Why?

Upvotes: 3

Views: 298

Answers (1)

Mundi
Mundi

Reputation: 80273

In iOS, there are the very convenient NSStringFromCGRect and CGRectFromNSString functions. Why not just use those and store a string?

Your questions:

  1. Yes, you need the 2 attributes, as explained in the documentation.
  2. Yes, this is based on the managed object model. The primitiveX name for x is generated / interpreted automatically.
  3. You need the primitive accessor methods here to make it KVC - which is not the case with primitives.

Upvotes: 1

Related Questions