iOS RestKit框架 - 发送请求后崩溃

时间:2012-07-04 05:15:47

标签: iphone objective-c ios http restkit

我的代码:

RKRequest.m:

#import "RKRequestDelegate.h"
#import "Constants.h"


@implementation RKRequestDelegate {

}

- (void)sendRequest:(UIImageView *)imageView {

    NSDictionary *queryParams = [NSDictionary dictionaryWithObjectsAndKeys:@"0", @"w", @"4", @"section", @"0",
                                                                           @"latitude", @"0", @"longitude", @"0", @"dt", @"0", @"di", nil];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:[Constants getSettingsName]
            ofType:[Constants getSettingsFormat]];
    NSDictionary *contentDictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
    NSString *addedUrl = [contentDictionary valueForKey:[Constants getBannerTag]];
    //

    RKRequest *request = [[RKClient sharedClient] get:addedUrl queryParameters:queryParams delegate:self];
}

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    NSLog(@"Response!!!");
}


@end

RKRequestDelegate.h:

@interface RKRequestDelegate : NSObject <RKRequestDelegate>

- (void)sendRequest:(UIImageView *)UIImageView;
- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response;

@end

AppDelegate.m:

    RKClient *client = [RKClient clientWithBaseURL:[HttpUtil getHost]];

启动应用程序后,它会在5秒后崩溃。如果我将delegate:self更改为委托:[RKRequestDelegate class]它不会崩溃,并且会给出响应200(确定),但它没有回调didLoadResponse

请帮助,谢谢。

更新:

如果我在RKRequestDelegate.m中使用[request sendSynchronously];,则会调用响应。

2 个答案:

答案 0 :(得分:2)

使request成为一个类变量。

编辑:这是一些代码,没有在现实生活中检查它,但这种方式应该有效

   @interface MYRequests : NSObject<RKRequestDelegate>
   {
       RKRequest *request;
   }

   @implementation MYRequests
   - (void)loginRequest
   {
       request = [RKRequest ...];
       request.method = RKRequestMethodGET;
       request.delegate = self;
       [request send];
   }
   @end

答案 1 :(得分:1)

你的崩溃几乎肯定是因为你的代表被过早释放了。如果您不想学习正确的内存管理,请尝试移动到等效的块:[RKClient get:usingBlock:],您可能会很幸运。