2012-07-04 70 views
0

我的代碼:的iOS 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

請大家幫忙,謝謝。

UPDATE:

如果我在RKRequestDelegate.m使用[request sendSynchronously];,響應被調用。

+0

你崩潰是幾乎可以肯定,因爲你委託的太早釋放。 –

回答

1

你的崩潰幾乎肯定是因爲你的委託被釋放得太早。如果您不想學習正確的內存管理,請嘗試移至相同的塊:[RKClient get:usingBlock:],您可能會很幸運。

2

使request一個類變量。

編輯:下面是一些代碼,沒有檢查它在現實生活中,但這種方式應該工作

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

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

我添加請求 @implementation BannerRKRequestDelegate { ... } 但不工作 – AIR

+0

應該在@接口部分,你的意思是這樣嗎? – anticyclope

+0

不,同樣的問題,只有[請求sendSynchronously]可以解決這個問題。 我在啓動應用程序(主屏幕),方法viewDidLoad,這是正確的? – AIR