RESTKit POST Request Tutorial

浪子不回头ぞ 提交于 2020-01-22 14:34:11

问题


I would like to know if there is a specific tutorial on how to do a POST request with RESTKit. I have looked at some tutorials but I haven't found any that say, "This is exactly how you do a POST request with RESTKit." Help is much appreciated.


回答1:


Assuming you already have a mapped model, you can simply perform this:

First, set a requestDescriptor with the inverseMapping of your responseDescriptor, assuming you have one with your mapping.

//This is used for mapping responses, you already should have one of this. PS:[Data mapping] is a method that returns an RKObjectMapping for my model. You should create yours or use a previous created one
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:[Data mapping] pathPattern:nil keyPath:@"data" statusCodes:statusCodeSet];
[[RKObjectManager sharedInstance] addResponseDescriptor:responseDescriptor];

//Inverse mapping, to perform a POST
RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:[[Data mapping] inverseMapping]  objectClass:[Data class] rootKeyPath:nil];
[[RKObjectManager sharedInstance] addRequestDescriptor:requestDescriptor];

After that, to perform a POST, just simply call the method below. Restkit will get the instance that you are trying to post, serialize it and send to the path chosen.

[[RKObjectManager sharedInstance] postObject:instanceOfYourModel path:yourPathHere parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    NSLog(@"Success");

} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    NSLog(@"Error");
}];

If you don't have a mapped model, let me know so we can try something else.



来源:https://stackoverflow.com/questions/17959723/restkit-post-request-tutorial

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