Skip to content

MR_dateValueForKeyPath will nil a value that it can't parse #1188

@gdunkle

Description

@gdunkle

My dates come down to the device via json as epoch milliseconds like this

  user =     {
        "_id" = 5451356be4b0c54793239842;
        "created" = 1414608235186;
}

I assumed this would not be a problem since I could just add an import function like the following

 func importCreated(value:AnyObject?)->Bool{
        if let epochMillis:String=value  as? String{
             let unixTimeStamp:NSTimeInterval = Double(epochMillis)! / 1000.0;
            self.created=NSDate.init(timeIntervalSince1970: unixTimeStamp);
            return true;
        }
        return false;
    }

The problem is the MR_dateValueForKeyPath method tries to convert the value and if it can't it just sets the value to nil.

I think the following change should be made to the MR_dateValueForKeyPath

- (NSDate *)MR_dateValueForKeyPath:(NSString *)keyPath fromObjectData:(id)objectData
{
    id value = [objectData valueForKeyPath:keyPath];
    if (![value isKindOfClass:[NSDate class]])
    {
        NSDate *convertedValue = nil;
        NSString *dateFormat;
        NSUInteger index = 0;
        do
        {
            NSString *dateFormatKey = kMagicalRecordImportCustomDateFormatKey;
            if (index)
            {
                dateFormatKey = [dateFormatKey stringByAppendingFormat:@".%tu", index];
            }
            index++;
            dateFormat = [[self userInfo] objectForKey:dateFormatKey];

            convertedValue = [value MR_dateWithFormat:dateFormat];

        } while (!convertedValue && dateFormat);
        //if the convertedValue is not equals to nil then update the value 
       //otherwise let the value flow through so that it can potentially be handled by an
      //import<AtrributeName> function
        if(convertedValue!=nil){
            value = convertedValue;
        }
    }
    return value;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions