0

我的代码:

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(OK),但它没有回调 a didLoadResponse

请帮忙,谢谢。

更新:

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

4

2 回答 2

2

创建request一个类变量。

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

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

   @implementation MYRequests
   - (void)loginRequest
   {
       request = [RKRequest ...];
       request.method = RKRequestMethodGET;
       request.delegate = self;
       [request send];
   }
   @end
于 2012-07-04T07:20:47.943 回答
1

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

于 2012-07-04T06:49:02.767 回答