RestKit:多个RKClients /更改baseURL

时间:2012-04-07 19:47:08

标签: restkit base-url

我在applicationDidFinishLaunching中的应用程序委托中初始化RKClient的sharedClient,它运行良好。我在大多数应用程序中使用此客户端目标URL,但是在1点,1类(播放器)中,我需要从gravatar.com加载用户的头像。所以,我让Player类定义它自己的RKClient并符合RKRequestDelegate协议。然后,它通过这个新的RKClient实例化发出请求,并将请求的委托设置为self。问题是我从未收到过回复;

- (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进行gravatar请求。但每当我尝试更改RKClient sharedClient的baseURL属性时,请使用以下任一方法:

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

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

我收到运行时错误:

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)

3 个答案:

答案 0 :(得分:1)

我用过:

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

这对我有用。

答案 1 :(得分:0)

clientWithBaseURL:方法需要NSString,而不是NSURL。尝试使用

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

答案 2 :(得分:0)

你只需要去目标 - >构建设置 - >其他链接标志

正如RestKit教程中所指定的,我们更改了添加了一个名为“-ObjC-all_load”的标志,现在编辑它只显示“-ObjC”

这必须为你做好准备。