How do I have two post routes for the same class in RestKit

随声附和 提交于 2020-01-01 03:36:12

问题


Since I couldn't figure out how to set up two different POST resource paths for the same class , I tried manually creating the RKObjectLoader request but it seems to keep sending a GET request instead of a POST even though I've set the method to POST. Here is my code

User *user = [[User alloc] init];
user.uname = uname;
user.pwd = pwd;


RKObjectManager *svc = [RKObjectManager sharedManager];
RKObjectMapping* mapping = [svc.mappingProvider objectMappingForClass:[User class]];

// what I was using before I needed two post resource paths//[svc postObject:user mapResponseWith:mapping delegate:self];

RKObjectLoader *loader = [svc loadObjectsAtResourcePath:authResourcePath objectMapping:mapping delegate:self];
[loader setMethod:RKRequestMethodPOST];
loader.userData = [NSNumber numberWithInt:RequestLogin];
loader.params = [NSDictionary dictionaryWithObjectsAndKeys:
                       uname, @"uname",
                       pwd, @"pwd",
                       nil];



[loader setSourceObject:user];
[loader send];
[user release];

回答1:


In cases where you have more than one path to POST or PUT to, the easiest thing to do is use the block form of the postObject: invocation and specify the destination resourcePath yourself:

[[RKObjectManager sharedManager] postObject:foo delegate:bar block:^(RKObjectLoader *loader) {
    loader.resourcePath = @"/my/destinationPath";
}];      

We may introduce a named route concept at some point that would let you disambiguate the routes using names, but for now its purely based on the HTTP verb.

Note that you do NOT and cannot register the secondary path on the router -- you are sidestepping it completely for the secondary path.




回答2:


In order to complete Blake Watters answer if the different route need different objectMapping you will need to do:

[[RKObjectManager sharedManager] postObject:query delegate:saveJobQueryHandler block:^(RKObjectLoader* loader) {
    loader.objectMapping = NEW_MAPPING;
    loader.resourcePath = @"/other/url";
    loader.targetObject = nil;  // Important
}];

For more information about loader.targetObject = nil; read sendObject:delegate:block:



来源:https://stackoverflow.com/questions/7450926/how-do-i-have-two-post-routes-for-the-same-class-in-restkit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!