Search code examples
iosobjective-ccore-dataentity-relationshipentity-attribute-value

CoreData: How to properly set created entity attributes


I have a Core Data Entity, let's call it "Record", and I have several attributes that I need to set with data from other objects/entities (as well as setting up relationships).

Let's say "Record" has the following attributes:

@interface Record (CoreDataProperties)

+ (NSFetchRequest<Record *> *)fetchRequest;

@property (nullable, nonatomic, copy) NSNumber *superRecordID;
@property (nullable, nonatomic, copy) NSNumber *otherDataID;
@property (nullable, nonatomic, copy) NSNumber *active;
@property (nullable, nonatomic, copy) NSDate *createDate;
@property (nullable, nonatomic, copy) NSDate *updateDate;
@property (nullable, nonatomic, copy) NSString *uuid;
@property (nullable, nonatomic, retain) SuperRecord *superRecord;

@end

I know for things such as active, I can set the default to 1 from XCode and for things such as createDate, updateDate and uuid, I can override the awakeFromInsert method.

My question is: what is the best practice for setting the other values and relationships upon creation? The only way I can think of that I know should work is to create the instance THEN set all the attributes, but is there a better way I can do this? Where I pass in the additional values/objects are parameters and set the attributes/relationships on creation?


Solution

  • Simply write a convenience allocator like this:

    - (instancetype)initWithSuperRecord:(SuperRecord*)superRecord active:(BOOL)active … context:(NSManagdObjectContext*)context
    {
      self = [super initWithEntity:… /* in subclasses you typically know that */ 
                    insertIntoManagedObjectContext:context];
      if( self ) 
      {
         // Set the properties you get from the args
      }
      return self;
    }
    
    + (instancetype)newWithSuperRecord:(SuperRecord*)superRecord active:(BOOL)active … context:(NSManagdObjectContext*)context
    {
       return [[self alloc] initWithSuperRecord:superRecord … context:context];
    }
    

    Some suggestions: The superID seems to be a property of the super record. Don't store it twice. Moreover you should check, whether you need an ID at all. This is not OOPish. Boolean values should by typed to boolean values. Use YES and NO or @YES and @NO instead of 1 and 0.

    Typed in Safari.