Created
October 18, 2013 19:08
-
-
Save guetz/7046533 to your computer and use it in GitHub Desktop.
Example to get post nested object in RestKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Test.h | |
// | |
// Created by Adam Guetz on 10/18/13. | |
// Copyright (c) 2013 Adam Guetz. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface Test : NSObject | |
+(void)test; | |
@end | |
@interface TestResponse : NSObject | |
@property (nonatomic, copy) NSNumber *userID; | |
@property (nonatomic, copy) NSString *username; | |
@property (nonatomic, copy) NSString *text; | |
@end | |
@interface User : NSObject | |
@property (nonatomic, copy) NSString *id; | |
@property (nonatomic, copy) NSString *firstname; | |
@property (nonatomic, copy) NSString *lastname; | |
@end | |
@interface Device : NSObject | |
@property (nonatomic, copy) NSString *token; | |
@end | |
@interface AuthCredentials : NSObject | |
@property (nonatomic) User *user; | |
@property (nonatomic) Device *device; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Test.m | |
// | |
// Created by Adam Guetz on 10/18/13. | |
// Copyright (c) 2013 Adam Guetz. All rights reserved. | |
// | |
#import "Test.h" | |
#import <RestKit.h> | |
#import <RKObjectManager.h> | |
@implementation TestResponse | |
- (NSString *)description | |
{ | |
return [NSString stringWithFormat:@"text: %@, name: %@, id: %@)", self.text, self.username, self.userID]; | |
} | |
@end | |
@implementation User | |
@end | |
@implementation AuthCredentials | |
@end | |
@implementation Device | |
@end | |
@implementation Test | |
+(void)test | |
{ | |
RKObjectManager *manager = [RKObjectManager managerWithBaseURL:[NSURL URLWithString:@"http://localhost:5002"]]; | |
RKObjectMapping *userMapping = [RKObjectMapping requestMapping]; | |
[userMapping addAttributeMappingsFromDictionary:@{ | |
@"id": @"id", | |
@"firstname": @"firstname", | |
@"lastname": @"lastname", | |
}]; | |
RKObjectMapping *deviceMapping = [RKObjectMapping requestMapping]; | |
[deviceMapping addAttributeMappingsFromDictionary:@{ | |
@"token": @"token" | |
}]; | |
RKObjectMapping *authMapping = [RKObjectMapping requestMapping]; | |
[authMapping addPropertyMapping:[RKRelationshipMapping | |
relationshipMappingFromKeyPath:@"user" | |
toKeyPath:@"user" | |
withMapping:userMapping]]; | |
[authMapping addPropertyMapping:[RKRelationshipMapping | |
relationshipMappingFromKeyPath:@"device" | |
toKeyPath:@"device" | |
withMapping:deviceMapping]]; | |
RKRequestDescriptor *authDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:authMapping objectClass:[AuthCredentials class] rootKeyPath:nil method:RKRequestMethodPOST]; | |
[manager addRequestDescriptor:authDescriptor]; | |
User *user = [[User alloc] init]; | |
user.id = @"77777-795C-4374-9A29-5C12FF8031F7"; | |
user.firstname = @"joe"; | |
user.lastname = @"bob"; | |
Device *device = [[Device alloc] init]; | |
device.token = @"phPS2sxbRRvoBbdE8e1VT8nFIJ4R8aJ6rdoCJ8iP1luWm3gUJDMnRHBs1SarfosTsIeYMMxwraLCY35B4whaFIISdosHzqa96vkZANHFy5rusHcrnUQuFgJHnsnqOsgxgOPd8QbrZAAxIlmqCt0an5pH69PWZAXcOVuzFilo310oeNW04qWZCWb8GWYMkODcMuUZANCAAFfvZB4ZAyokBADz"; | |
AuthCredentials *cred = [[AuthCredentials alloc] init]; | |
cred.user = user; | |
cred.device = device; | |
RKObjectMapping *ResponseMapping = [RKObjectMapping mappingForClass:[TestResponse class]]; | |
[ResponseMapping addAttributeMappingsFromDictionary:@{ | |
@"user.name": @"username", | |
@"user.id": @"userID", | |
@"text": @"text" | |
}]; | |
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:ResponseMapping method:RKRequestMethodAny pathPattern:nil keyPath:nil statusCodes:nil]; | |
[manager addResponseDescriptor:responseDescriptor]; | |
[manager postObject:cred path:@"/testpost" parameters:nil | |
success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) | |
{ | |
NSLog(@"TestResponse: %@", [[[mappingResult array] firstObject] description]); | |
} | |
failure:^(RKObjectRequestOperation *operation, NSError *error) | |
{ | |
}]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment