RestKit - mapping a User object in an example application

I have a DiscussionBoard application running for RestKit and I am transferring the DBUser class to my application. The only difference is that my application will not use CoreData.

I can do a POST user object and return a new user id and authentication_token in my application. The problem is that the response does not serialize to the user object. Why request:didLoadResponse:response does not return a User object, but I can see the correct json in signUpWithDelegate:delegate

And you can see here, I can't get objectLoader:didLoadObjects to return using serialized objects? Why is the object not serializable?

  [12881:207]Loaded payload: {"user":{"id":"85","authentication_token":"_Udl98OO-xgmZOOJM26w","email":" ffff@adsfadfa.com "}} [12881:207] _____ objectLoader didLoadObjects ( ) [12881:207] loginWasSuccessful (null) 

AppDelegate.m

 RKObjectManager *objectManager = [RKObjectManager objectManagerWithBaseURL:@"http://spoonbook-2.local:3000/"]; [objectManager.router routeClass:[RKUser class] toResourcePath:@"/api/v1/authentication/create.json" forMethod:RKRequestMethodPOST]; // User RKObjectMapping *userMapping = [RKObjectMapping mappingForClass:[RKUser class]]; [userMapping mapKeyPath:@"id" toAttribute:@"userId"]; [userMapping mapAttributes:@"email", nil]; [userMapping mapAttributes:@"authentication_token", nil]; [objectManager.mappingProvider setObjectMapping:userMapping forKeyPath:@"user"]; 

RegisterViewController.m

  -(IBAction)handleLoginClick:(id)sender { NSLog(@"handleLoginClick"); RKUser *user = [[[RKUser alloc] init] retain]; [user createWithEmail:_regEmailTF.text andPassword:_regPasswordTF.text delegate:self]; [user release]; } 

User.m

 #import "RKUser.h" #import <RestKit/RestKit.h> static NSString* const kDBUserCurrentUserIDDefaultsKey = @"kDBUserCurrentUserIDDefaultsKey"; // Current User singleton static RKUser* currentUser = nil; @implementation RKUser @synthesize userID = _userID; @synthesize email = _email; @synthesize password = _password; @synthesize passwordConfirmation = _passwordConfirmation; @synthesize delegate = _delegate; @synthesize authentication_token = _authentication_token; -(void)createWithEmail:(NSString *)username andPassword:(NSString *)password delegate:(NSObject<UserAuthenticationDelegate> *)delegate { _delegate = delegate; RKObjectManager*objectManager = [RKObjectManager sharedManager]; RKObjectLoader* objectLoader = [objectManager objectLoaderWithResourcePath:@"/api/v1/authentication/create.json" delegate:self]; objectLoader.method = RKRequestMethodPOST; objectLoader.params = [NSDictionary dictionaryWithKeysAndObjects:@"user[email]", username, @"user[password]", password, @"user[password_confirmation]", password, nil]; objectLoader.targetObject = self; // try adding a userMapping? RKObjectMapping *userMapping = [objectManager.mappingProvider objectMappingForKeyPath:@"user"]; objectLoader.objectMapping = userMapping; // TODO: Temporary to work around bug in Rails serialization on the Heroku app objectLoader.objectMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForKeyPath:@"user"]; [objectLoader send]; } - (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response { NSLog(@"ReviewFormViewController Loaded payload: %@", [response bodyAsString]); } - (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray *)objects { // NOTE: We don't need objects because self is the target of the mapping operation NSLog(@"_____ objectLoader didLoadObjects %@", objects); if ([objectLoader wasSentToResourcePath:@"/api/v1/authentication/login.json"]) { // Login was successful [self loginWasSuccessful]; } else if ([objectLoader wasSentToResourcePath:@"/api/v1/authentication/create.json"]) { // Sign Up was successful if ([self.delegate respondsToSelector:@selector(userDidSignUp:)]) { [self.delegate userDidSignUp:self]; } // Complete the login as well [self loginWasSuccessful]; } } - (void)objectLoader:(RKObjectLoader *)objectLoader didFailWithError:(NSError*)error { ...snip... } - (void)loginWasSuccessful { ...snip... } @end 
+4
source share
1 answer

I think you need to use this to be able to display the answer.

 /** Send the data for a mappable object by performing an HTTP POST. The data returned in the response will be mapped according to the object mapping provided. */ - (RKObjectLoader*)postObject:(id<NSObject>)object mapResponseWith:(RKObjectMapping*)objectMapping delegate:(id<RKObjectLoaderDelegate>)delegate; 
+6
source

All Articles