4

私はDiscussionBoardアプリをRestKitで動作させており、 DBUserクラスをアプリケーションに移植しています。私のアプリの唯一の違いは、CoreDataを使用しないことです。

ユーザーオブジェクトをPOSTして、新しいユーザーIDとauthentication_tokenをアプリに返すことができます。問題は、応答がユーザーオブジェクトにシリアル化されていないことです。request:didLoadResponse:responseUserオブジェクトを返さないのに、正しいjsonが表示されるのはなぜですかsignUpWithDelegate:delegate

objectLoader:didLoadObjectsそして、あなたはここで私がシリアル化されたオブジェクトで戻る ことができないのを見ることができますか?オブジェクトがシリアル化されないのはなぜですか?

  [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

1 に答える 1

6

応答をマッピングできるようにするには、これを使用する必要があると思います。

/**
 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;
于 2011-07-18T18:03:57.440 に答える