ios – RestKit请求值变换器?

ios – RestKit请求值变换器?,第1张

概述我在使用RKValueTransformer将NSData图像字节序列化为请求的base64编码字符串时遇到一些问题.在 some help I received on stackoverflow之后,我能够做出反应. 这是我创建NSString到NSData值转换器的代码,它可以正常工作.我找到了null值转换器的索引并将其设置为afterNullTransformerIndex.我也将它设置为 我在使用RKValuetransformer将NSData图像字节序列化为请求的base64编码字符串时遇到一些问题.在 some help I received on stackoverflow之后,我能够做出反应.

这是我创建Nsstring到NSData值转换器的代码,它可以正常工作.我找到了null值转换器的索引并将其设置为afterNulltransformerIndex.我也将它设置为索引0,但后来我必须进行自己的空检查,这似乎没有问题.

//add the base64 to NSData transformer after the null value transformerRKBlockValuetransformer *base64StringToNSDatatransformer = [RKBlockValuetransformer valuetransformerWithValIDationBlock:^BOol(__unsafe_unretained Class inputValueClass,__unsafe_unretained Class outputValueClass) {    return [inputValueClass isSubclassOfClass:[Nsstring class]] && [outputValueClass isSubclassOfClass:[NSData class]];} transformationBlock:^BOol(ID inputValue,__autoreleasing ID *outputValue,__unsafe_unretained Class outputClass,NSError *__autoreleasing *error) {    RKValuetransformerTestinputValueIsKindOfClass(inputValue,[Nsstring class],error);    RKValuetransformerTestOutputValueClassIsSubclassOfClass(outputClass,[NSData class],error);    *outputValue = [[NSData alloc] initWithBase64EncodedString:(Nsstring *)inputValue options:NSDataBase64DeCodingIgnoreUnkNownCharacters];    return YES;}];base64StringToNSDatatransformer.name = @"base64StringToNSDatatransformer";[[RKValuetransformer defaultValuetransformer] insertValuetransformer:base64StringToNSDatatransformer atIndex:afterNulltransformerIndex];

这是我创建NSData到Nsstring值转换器的代码,它不起作用.我在transformationBlock:方法中设置了一个断点,但它永远不会被调用:

//add the NSData to String transformer for requests after the null value transformerRKBlockValuetransformer *nsDataToBase64Stringtransformer = [RKBlockValuetransformer valuetransformerWithValIDationBlock:^BOol(__unsafe_unretained Class inputValueClass,__unsafe_unretained Class outputValueClass) {    return [inputValueClass isSubclassOfClass:[NSData class]] && [outputValueClass isSubclassOfClass:[Nsstring class]];} transformationBlock:^BOol(ID inputValue,error);    *outputValue = [((NSData *)inputValue) base64EncodedStringWithOptions:NSDataBase64EnCoding76CharacterlineLength];    return YES;}];nsDataToBase64Stringtransformer.name = @"nsDataToBase64Stringtransformer";[[RKValuetransformer defaultValuetransformer] insertValuetransformer:nsDataToBase64Stringtransformer atIndex:afterNulltransformerIndex];

就像我说的那样,我的断点永远不会在transformationBlock:方法中被调用,但是valuetransformationWithValIDationBlock:在序列化请求时会被调用一次,但只有在从Date转换为String时才会被调用.通过调试器和RestKit的代码查看堆栈,我在RKObjectParameterization.m中找到了这个方法:

- (voID)mapPingOperation:(RKMapPingOperation *)operation dIDSetValue:(ID)value forKeyPath:(Nsstring *)keyPath usingMapPing:(RKAttributeMapPing *)mapPing{    ID transformedValue = nil;    if ([value isKindOfClass:[NSDate class]]) {        [mapPing.objectMapPing.valuetransformer transformValue:value tovalue:&transformedValue ofClass:[Nsstring class] error:nil];    } else if ([value isKindOfClass:[NSDecimalNumber class]]) {        // Precision numbers are serialized as strings to work around JavaScript notation limits        transformedValue = [(NSDecimalNumber *)value stringValue];    } else if ([value isKindOfClass:[NSSet class]]) {        // NSSets are not natively serializable,so let's just turn it into an NSArray        transformedValue = [value allObjects];    } else if ([value isKindOfClass:[NSOrderedSet class]]) {        // NSOrderedSets are not natively serializable,so let's just turn it into an NSArray        transformedValue = [value array];    } else if (value == nil) {        // Serialize nil values as null        transformedValue = [NSNull null];    } else {        Class propertyClass = RKPropertyInspectorGetClassForPropertyAtKeyPathOfObject(mapPing.sourceKeyPath,operation.sourceObject);        if ([propertyClass isSubclassOfClass:NSClassFromString(@"__NSCFBoolean")] || [propertyClass isSubclassOfClass:NSClassFromString(@"NSCFBoolean")]) {            transformedValue = @([value boolValue]);        }    }    if (transformedValue) {        RKLogDeBUG(@"Serialized %@ value at keyPath to %@ (%@)",NsstringFromClass([value class]),NsstringFromClass([transformedValue class]),value);        [operation.destinationObject setValue:transformedValue forKeyPath:keyPath];    }}

只有当值是NSDate时,RestKit才会使用值变换器!是否有一些我缺少的东西让价值变换器处理请求?

编辑回答Wain的问题并提供更多细节

这是我的实体映射响应代码.记录实体拥有WTSImages的集合:

RKEntityMapPing *imageMapPing = [RKEntityMapPing mapPingForEntityForname:@"WTSImage" inManagedobjectStore:self.managedobjectStore];[imageMapPing addAttributeMapPingsFromDictionary:@{                                                   @"ID": @"dbID",@"status": @"status",@"type": @"type",@"format": @"format",@"wIDth": @"wIDth",@"height": @"height",@"image": @"imageData"                                                   }];imageMapPing.IDentificationAttributes = @[@"dbID"];[recordMapPing addPropertyMapPing:[RKRelationshipMapPing relationshipMapPingFromKeyPath:@"images" toKeyPath:@"images" withMapPing:imageMapPing]];

WTSImage类是从CoreData生成的,如下所示:

@interface WTSImage : NSManagedobject@property (nonatomic,retain) NSNumber * dbID;@property (nonatomic,retain) Nsstring * format;@property (nonatomic,retain) NSNumber * height;@property (nonatomic,retain) NSData * imageData;@property (nonatomic,retain) Nsstring * status;@property (nonatomic,retain) Nsstring * type;@property (nonatomic,retain) NSNumber * wIDth;@property (nonatomic,retain) WTSCaptureDevice *captureDevice;@property (nonatomic,retain) WTSRecord *record;@property (nonatomic,retain) WTstempimageset *tempimageset;@end

我创建了一个反向记录映射并添加了一个请求描述符.

RKEntityMapPing *reverseRecordMapPing = [recordMapPing inverseMapPing];[self addRequestDescriptor:[RKRequestDescriptor requestDescriptorWithMapPing:reverseRecordMapPing objectClass:[WTSRecord class] rootKeyPath:@"records" method:RKRequestMethodAny]];

这是用于将我的图像对象映射到JsON的调试日志输出. imageData元素看起来不像普通的base64编码字符串:

2014-04-10 11:02:39.537 IDentify[945:60b] T restkit.object_mapPing:RKMapPingOperation.m:682 Mapped relationship object from keyPath 'images' to 'images'. Value: (    {    format = JPEG;    height = 200;    ID = 0;    image = <ffd8ffe0 00104a46 49460001 01000001 00010000 ffe10058 45786966 ... f77d7bf9 77b58fff d9>;    status = C;    type = MUGSHOT;    wIDth = 200;})

这是POST,我的服务器拒绝:

2014-04-10 11:27:53.852 IDentify[985:60b] T restkit.network:RKObjectRequestoperation.m:148 POST 'http://10.0.0.35:8080/Service/bs/records':request.headers={    Accept = "application/Json";    "Accept-Language" = "en;q=1,es;q=0.9,fr;q=0.8,de;q=0.7,ja;q=0.6,nl;q=0.5";    "Content-Type" = "application/x-www-form-urlencoded; charset=utf-8";    "User-Agent" = "IDentify/1.0 (iPhone; iOS 7.1; Scale/2.00)";}request.body=records[application]=IDentify&records[createBy]=welcomed&records[createDt]=2014-04-10T15%3A27%3A42Z&records[description]&records[externalID]&records[groupID]=5&records[ID]=0&records[images][][format]=JPEG&records[images][][height]=200&records[images][][ID]=0&records[images][][image]=%3Cffd8ffe0%2000104a46%2049460001%2001000001%20000.......d773%20ffd9%3E&records[images][][status]=C&records[images][][type]=MUGSHOT&records[images][][wIDth]=200&records[locked]&records[modifyBy]&records[modifyDt]&records[priv]
解决方法

In RKObjectMapPing classForKeyPath:,it is unable to find the class for my ‘image’ property. It appears that the _objectClass is a NSMutableDictionary rather than a WTSImage. This is causing the method to return a nil propertyClass

这是有道理的,因为请求的映射目的地是NSMutableDictionary(并且源对象是WTSImage.因此,它不应用任何特定的转换并落到mapPingOperation:dIDSetValue:forKeyPath:usingMapPing:你已经看到它没有’以迎合这种情况.

我认为使用变压器很难处理.

我现在能想到处理它的唯一方法是向WTSImage添加一个方法,比如base64Image,它返回转换后的图像数据并在你的映射中使用它(这意味着你将无法使用[recordMapPing inverseMapPing]) .

总结

以上是内存溢出为你收集整理的ios – RestKit请求值变换器?全部内容,希望文章能够帮你解决ios – RestKit请求值变换器?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1071928.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存