Restkit: migrating to 0.20

帅比萌擦擦* 提交于 2019-12-23 12:09:56

问题


I am trying to migrate to RestKit 0.20-pre2.

Currently I managed to migrate my mapping (at least the compiler does not complain anymore), but I have problems in creating requests (previously I used the RKObjectLoader which does not exist anymore.

My previous code is the following:

- (RKObjectLoader*)objectLoaderWithResourcePath: (NSString*)resourcePath
                                     method: (RKRequestMethod)httpMethod
                                 parameters: (NSDictionary*)parameters
                              mappableClass: (Class)objectClass
{
RKObjectMapping *mapping = [self.objectManager.mappingProvider     objectMappingForClass:objectClass];

NSString *path = resourcePath;
if (httpMethod == RKRequestMethodGET) {
    path = [resourcePath stringByAppendingQueryParameters:parameters];
}
RKObjectLoader *request = [self.objectManager loaderWithResourcePath:path];
request.method = httpMethod;
request.delegate = self;
request.objectMapping = mapping;
if (httpMethod != RKRequestMethodGET) {
    request.params = parameters;
}

return request;
}

I used the above method to create a generic request, and then send it either synchronously or asynchronously. Now... I saw the new method getObjectsAtPath: parameters: success: failure:, but.. I need the same for the POST (and I don't have any object to post... it is simply the server which accept a POST request for the login..)

Any help?

Thank you


回答1:


I had the same problem as you and i received a great answer here:

Trying to make a POST request with RestKit and map the response to Core Data

Basically,

This is what you need:

 NSDictionary *dictionary = @{ @"firstParam": @(12345), @"secondParam": @"whatever"};
 NSMutableURLRequest *request = [objectManager requestWithObject:nil     method:RKRequestMethodPOST path:@"/whatever" parameters:parameters];

 RKObjectRequestOperation *operation = [objectManager  objectRequestOperationWithRequest:request ^(RKObjectRequestOperation *operation,     RKMappingResult *result) {
NSLog(@"Loading mapping result: %@", result);
  } failure:nil];

There is an example here in README section Managed Object Request that will help you:

https://github.com/RestKit/RestKit




回答2:


You can use AFNetworking directly using the RK HTTPClient subclass, something like this:

[[RKObjectManager sharedManager].HTTPClient postPath:@"/auth" parameters:params success:^(AFHTTPRequestOperation *operation, id JSON)
 {
     // Success
 } failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     // Error
 }];

Since RestKit v0.20.x, RK now use AFNetworking under the hood instead of RKClient, so you can refer directly to the AFNetworking docs:

http://afnetworking.github.com/AFNetworking/Classes/AFHTTPClient.html#//api/name/postPath:parameters:success:failure:

Edit

In my project, for the auth, I simply created an NSObject named User, with a singleton, and managed the mapping myself. I (personally) didn't need to have my auth user in my core data stack. If you need to use the RK Core data mapping capabilities, take a look at RKObjectManager with the postObject:path:parameters:success:failure: method.



来源:https://stackoverflow.com/questions/13722830/restkit-migrating-to-0-20

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