⬆️ ⬇️

RestKit - description of one of the possibilities

Hi Habr!



Prehistory





It has long been noted that there is not a single article on Habré about such a wonderful library as RestKit , written in Objective-C and providing excellent opportunities for working with RESTful services. Therefore, the idea came to publish a brief description of some of the features of the library, especially since a completely new version of it has already been released using the library for working with HTTP as the basis for AFNetworking . The article describes one of the most important functionalities for me - object mapping.

')

The process of installing the library is described in detail on the project page on GitHub. The same library is available in Pods:

$ cat Podfile platform :ios, '5.0' pod 'RestKit', '~> 0.20.0pre' 


Let's get down to business



We can do without a theory, because the article is for those who understand, at least, what REST is and why it is needed.



Initialization



At the start of the application, it is necessary to set the initial parameters for singletons working with HTTP and mapping objects:

 NSURL *baseURL = [NSURL URLWithString:kServerHost]; AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client]; [RKObjectManager setSharedManager:objectManager]; 




Mapping objects


First we need to describe the class in which the data will be stored (where they will be mapped):

 @interface TaskListSuccess : NSObject @property(nonatomic, copy) NSString *created_at; @property(nonatomic, copy) NSString *updated_at; @property(nonatomic, copy) NSString *user_id; @property(nonatomic, copy) NSString *ID; @property(nonatomic, copy) NSString *title; @property(nonatomic, copy) NSString *time; @end 


Next, you need to add a rule for the answers for which queries this class will be used and by what rule the objects are mapped:

 RKObjectMapping* successAuthResult = [RKObjectMapping mappingForClass:[TaskListSuccess class]]; [successAuthResult addAttributeMappingsFromDictionary:@{ //    JSON(XML)    @"created_at": @"created_at", @"updated_at": @"updated_at", @"user_id": @"user_id", @"id": @"ID", @"title": @"title", @"time": @"time", }]; //  -,      . NSIndexSet *successStatusCodes = RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful); RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:successAuthResult pathPattern:@"tasks" keyPath:nil statusCodes:successStatusCodes]; [[RKObjectManager sharedManager] addResponseDescriptor:responseDescriptor]; 


The pathPattern parameter of the RKResponseDescriptor class describes the pattern of the path for which our class will be used for mapping.

Next you need to send the request itself:

 RKObjectManager *objectManager = [RKObjectManager sharedManager]; [objectManager getObject:nil path:@"tasks" parameters:nil success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { //    ... } failure:^(RKObjectRequestOperation *operation, NSError *error) { //    ... }]; 


This is the simplest query, sometimes we need more flexibility in customizing the query, then we can do this, for example, as follows:

 RKObjectManager *objectManager = [RKObjectManager sharedManager]; NSMutableURLRequest *urlRequest = [objectManager requestWithObject:nil method:RKRequestMethodPOST path:@"tasks" parameters:nil]; [urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; RKObjectRequestOperation *operation = [objectManager objectRequestOperationWithRequest:urlRequest success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { //    ... } failure:^(RKObjectRequestOperation *operation, NSError *error) { //    ... }]; [objectManager enqueueObjectRequestOperation:operation]; 


An object of class RKObjectRequestOperation contains all possible parameters that we can set as we please.



Outro



The article is introductory and aims to understand whether it is important to describe this library further, especially since there is very good documentation in English. In general, if this topic is of interest, then it will be possible to go back to it and describe other possibilities of the library, as well as tell about the personal cones stuffed during its use.



Should I describe the library further?



Opportunities:



All the best! World!

Source: https://habr.com/ru/post/172233/



All Articles