【问题标题】:Mapping RestKit Post Response映射 RestKit 后响应
【发布时间】:2012-04-05 22:47:20
【问题描述】:

我正在使用 RestKit 发布到 uri

    NSString* path = [NSString stringWithFormat:@"myPathHere"];
    NSDictionary *params = [NSDictionary dictionaryWithKeysAndObjects:@"rating",[rating stringValue], nil];
   [[RKObjectManager sharedManager].client post:path params:params delegate:self];

服务器将响应我的 managedObject“项目”的更新版本。

我该如何映射这个项目。我是否使用以下委托方法?

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response{}

我知道从这里我可以获取响应 JSON 值,但是如何将其更改为映射对象?

非常感谢任何帮助。

谢谢!

【问题讨论】:

    标签: objective-c ios mapping restkit


    【解决方案1】:

    当我需要映射我的服务器对象时,我会发出这样的请求:

            [[RKObjectManager sharedManager] 
             loadObjectsAtResourcePath:[qualifiedResourceURL
                                        appendQueryParams:params] 
             delegate:self  
             ];
    

    然后对象在这个方法中返回:

        - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects 
    

    然后我使用通知管理器发布更新的对象

            [[NSNotificationCenter defaultCenter] postNotificationName:GotFotosInRegion_Fotos_Array_Response object:objects];
    

    最好的文档在https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md

    【讨论】:

    • 这在我使用 GET 但我使用 POST 时有效。如果您查看 applyQueryParams 方法,它所做的就是获取您传入的字典并使用“?”将其附加到路径中。在中间。
    • 你能把你的 RKObjectRouter 设置为 POST。像这样: router = [RKObjectRouter new] ; [router routeClass:[CheckStatus class] toResourcePath:@"/registration/rest/users/checkstatus" forMethod:RKRequestMethodPOST];
    • 我已经尝试过了,但它似乎不起作用。我觉得我越来越近了。以下 (在下一条评论中) 将导致 objectloader didloadobjects 被调用但对象数组为空。
    • NSString* path = [NSString stringWithFormat:@"/PATH/HERE",theItem.entityId, theItem.identifier]; RKObjectManager*objectManager = [RKObjectManager sharedManager]; RKObjectLoader* objectLoader = [objectManager objectLoaderWithResourcePath:path delegate:self]; objectLoader.method = RKRequestMethodPOST; objectLoader.params = [NSDictionary dictionaryWithKeysAndObjects:@"rating",[rating stringValue], nil]; objectLoader.targetObject = self; objectLoader.objectMapping = [ObjectMappings mappingForMenu]; [objectLoader send];
    • 这会稍微让你绊倒。您是否使用了代理? Charles Proxy 在我安装它的那天为我节省了几个小时。
    猜你喜欢
    • 1970-01-01
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 2014-08-13
    相关资源
    最近更新 更多