RestKit/RestKit — Issue #565: Key-value validation does not allow value modification
Author: jcanfield55
Created: 2012-02-23 04:30:57+00:00
State: open

Thanks everyone who has created this great library. It is a big help!

I am having a problem with key-value validation. I want to modify the value of a date attribute because the RESTful API I am using returns dates in milliseconds since 1970 as a straight number (not the dotNet format). Since the mapping interprets this as a number, not a string, RESTKit won't call a custom date formatter. So I thought I'd solve the problem using key-value validation and modify the date back to the proper value.

My validator routine is called and creates the correct date, but RestKit does not use the new NSDate object I create. I checked it out in the debugger, and here is what is happening in RKObjectMappingOperation:
- validateValue method gets the new date value, because it passed &value
- but when validateValue returns control back to shouldSetValue, the new value is lost, because value was not passed by reference.

Here is the code I used for validation:

- (BOOL)validateDate:(__autoreleasing id *)ioValue error:(NSError *__autoreleasing *)outError
{
if ([*ioValue isKindOfClass:[NSDate class]]) {
NSDate* ioDate = *ioValue;
NSDate* farFutureDate = [NSDate dateWithTimeIntervalSinceNow:(60.0*60*24*365*1000)]; // 1,000 years in future
if ([ioDate laterDate:farFutureDate]==ioDate) { // if date is >1,000 years in future, divide time since 1970 by 1000
NSDate* newDate = [NSDate dateWithTimeIntervalSince1970:([ioDate timeIntervalSince1970] / 1000.0)];
*ioValue = newDate;
}
return YES;
}
return NO;
} */

Thanks for your help, and I welcome any other suggestions besides key-value Validation for robustly handling milliseconds for dates.

John