ios - RestKit valueTransformer not being called -
i using restkit talk json endpoints. endpoint returns unix timestamp in 'number of milliseconds'. restkit's default transformer assumes 'number of seconds' , wrong value in nsdate.
so looked around, , found need utilize custom valuetransformer tell restkit how transform timestamp. here's code have.
+ (rkblockvaluetransformer*) timeintervalinmillisecondssince1970twowaytransformer { homecoming [rkblockvaluetransformer valuetransformerwithvalidationblock:^bool(__unsafe_unretained class sourceclass, __unsafe_unretained class destinationclass) { // transformer handles `nsnumber` <-> `nsdate` transformations nslog(@"checking new transformer"); homecoming (([sourceclass issubclassofclass:[nsnumber class]] && [destinationclass issubclassofclass:[nsdate class]]) || ([sourceclass issubclassofclass:[nsdate class]] && [destinationclass issubclassofclass:[nsnumber class]])); } transformationblock:^bool(id inputvalue, __autoreleasing id *outputvalue, __unsafe_unretained class outputvalueclass, nserror *__autoreleasing *error) { nslog(@"transforming"); rkvaluetransformertestinputvalueiskindofclass(inputvalue, (@[ [nsnumber class], [nsdate class] ]), error); rkvaluetransformertestoutputvalueclassissubclassofclass(outputvalueclass, (@[ [nsnumber class], [nsdate class] ]), error); if ([outputvalueclass issubclassofclass:[nsdate class]]) { if ([inputvalue iskindofclass:[nsnumber class]]) { *outputvalue = [nsdate datewithtimeintervalsince1970:[inputvalue doublevalue] / 1000]; } } else if ([outputvalueclass issubclassofclass:[nsnumber class]]) { *outputvalue = @((nsinteger)[inputvalue timeintervalsince1970] * 1000); } homecoming yes; }]; }
then adding transformer restkit's default transformer.
rkvaluetransformer* transformer = [self timeintervalinmillisecondssince1970twowaytransformer]; [[rkvaluetransformer defaultvaluetransformer] addvaluetransformer:transformer];
but, transformer never gets called. nslog
statement wrote in never gets executed!
so tried -- writing transformer on attributemapping this:
rkvaluetransformer* transformer = [self timeintervalinmillisecondssince1970twowaytransformer]; rkattributemapping *tokenexpirymapping = [rkattributemapping attributemappingfromkeypath:@"access_token_expiration" tokeypath:@"accesstokenexpirationdate"]; tokenexpirymapping.valuetransformer = transformer; [usermapping addpropertymapping:tokenexpirymapping];
but code doesn't compile when this. says "property valuetransformer not found on object of type 'rkattributemapping *'". don't understand this. examples have seen on internet, including 1 restkit's documentation same thing. examples set valuetransformer
property of attributemapping
object. restkit class reference says can set it. why doesn't compile?
so managed figure out problem was.
my podfile configured utilize restkit 0.20.3. , had added rkvaluetransformers
pod. however, restkit 0.20.3 , before come own version of rkvaluetransformer (.h , .m) files. , older versions of restkit doesn't back upwards adding own transformers, because doesn't create utilize of newer rkvaluetransformers
library.
when upgraded restkit version newest version (actually above 0.21.0 work), things started working fine. didn't need add together pod rkvaluetransformers, because gets added automatically dependency of restkit.
ios objective-c restkit restkit-0.20 nsvaluetransformer
No comments:
Post a Comment