2012-04-07 5 views
0

私は、アプリケーションデリゲートのapplicationDidFinishLaunchingでRKClientのsharedClientを初期化します。私はこのクライアントのリンク先URLをアプリケーションのほとんどに使用していますが、1つのクラス(Player)の1ポイントでgravatar.comからユーザーのアバターを読み込む必要があります。だから、私はPlayerクラスが独自のRKClientを定義し、RKRequestDelegateプロトコルに準拠させるようにしました。次に、この新しいRKClientインスタンスを使用して要求を行い、要求の代理人を自己に設定します。問題は私が決して応答を受け取らないということです。すなわち、RestKit:複数のRKClients/baseURLを変更する

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response 

決して呼び出されません。

// Player.m 

#import "Player.h" 

@implementation Player 

# pragma mark - Accessor Synthesizers 

@synthesize identifier = _identifier; 
@synthesize name = _name; 
@synthesize story = _story; 
@synthesize emailHash = _emailHash; 
@synthesize pointPercentage = _pointPercentage; 
@synthesize hitPercentage = _hitPercentage; 
@synthesize lastCups = _lastCups; 
@synthesize shotCount = _shotCount; 
@synthesize hitCount = _hitCount; 
@synthesize wins = _wins; 
@synthesize losses = _losses; 
@synthesize gravatar = _gravatar; 

# pragma mark - Instance Methods 

- (void)getGravatar { 
    RKClient *gClient = [RKClient clientWithBaseURL:[NSURL URLWithString:@"http://gravatar.com"]]; 
    NSString *path = [self gravatarLink]; 
    NSLog(@"Getting Gravatar With Link: http://gravatar.com%@", path); 
    [gClient get:path delegate:self]; 
} 

- (NSString *)description { 
    return [NSString stringWithFormat:@"{Season: [id=%i, name=%@, story=%@ ]}", _identifier, _name, _story]; 
} 

# pragma mark - RKRequest Delegate Methods 

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response { 
    NSLog(@"Gravatar Back: %@", response.bodyAsString); 
    self.gravatar = response.body; 
    [[NSNotificationCenter defaultCenter] postNotificationName:@"GravatarLoaded" object:self]; 
} 

# pragma mark - Private Methods 

- (NSString *)gravatarLink { 
    NSString *path; 
    path = [NSString stringWithFormat:@"/avatar/%@?d=monsterid&r=x", _emailHash]; 
    if([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale]==2) 
     return [path stringByAppendingString:@"&s=200"]; 
    else 
     return [path stringByAppendingString:@"&s=100"]; 
} 

@end 

はまた、私はsharedClientのベースURLを変更し、ちょうどグラバター要求のためsharedClientを使用して試してみました。ここでは、全体のコードサンプルです。私はこれらのいずれかの方法で、RKClient sharedClientのベースURLプロパティを変更しようとするたびしかし:

2012-04-07 15:37:23.565 MLP[34403:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSURL URLByAppendingResourcePath:queryParameters:]: unrecognized selector sent to instance 0x8bcdd50' 
*** First throw call stack: 
(0x1b7c022 0x1f58cd6 0x1b7dcbd 0x1ae2ed0 0x1ae2cb2 0x23eb5 0x24032 0x6ddb 0xdda2 0xc465c5 0xc467fa 0x14db85d 0x1b50936 0x1b503d7 0x1ab3790 0x1ab2d84 0x1ab2c9b 0x20407d8 0x204088a 0xbb5626 0x27cd 0x2735) 
terminate called throwing an exception(lldb) 

答えて

0

clientWithBaseURL:方法:

[[RKClient sharedClient] setBaseURL:[NSURL URLWithString:@"http://gravatar.com"]]; 

または

[RKClient sharedClient].baseURL: [NSURL URLWithString:@"http://gravatar.com"]; 

私は、ランタイムエラーを取得しますNSURLではなく、NSStringが必要です。編集今や「-ObjC-all_load」と呼ばれるフラグを追加しました>他のリンカフラグ

RestKitチュートリアルで指定されているように、我々は変更 - [設定]を構築>あなただけのTargets-に行く必要が

[[RKClient sharedClient] setBaseURL:@"http://gravatar.com"]; 
0

を使用してみてくださいそれは単に「-ObjC」これはあなたのためWOKしなければならない

を表示します。

1

は、私が使用:私のために働い

[[RKClient sharedClient] setBaseURL:[RKURL URLWithString:@"http://gravatar.com"]];