magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

MagicalRecord imports keys absent in payload / MR 3.0 #1160

Open pronebird opened 8 years ago

pronebird commented 8 years ago

I have a managed object subclass that updates updatedAt property upon insertion into context. updatedAt is set as required and should not be nil, ever. I automatically manage this field in managed object subclass with help of awakeFromInsert and willSave.

When I use MR_importFromArray, MagicalRecord attempts to set nil for attributes absent in payload which in my case leads to save errors because awakeFromInsert updates this property and then MR erases it with nil during import.

I think MagicalRecord should not attempt to set attributes absent in payload and let CoreData automatically handle that. Therefore, I think there is no need to check for MR_shouldUseDefaultValueIfNoValuePresent and extract default values from attribute.

I am definitely going to change my model but I think there is some sense in this for fields that are required but precomputed automatically.

Code snippet from current implementation in MR 3.0:

NSString *attributeName = [attributeInfo name];

id value = [attributeInfo MR_valueForKeyPath:lookupKeyPath fromObjectData:objectData];
if (value == nil && [attributeInfo MR_shouldUseDefaultValueIfNoValuePresent])
{
    value = [attributeInfo defaultValue]; // let CoreData handle this maybe?
}

//        id value = [attributeInfo MR_valueForKeyPath:lookupKeyPath fromObjectData:objectData];
if (![self MR_importValue:value forKey:attributeName])
{
    [self setValue:value forKey:attributeName];
}