AIR
AIR

Reputation: 1

iOS RestKit Framework - crashing after sent request

my code:

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]];

After launch application it's crashes after 5 seconds. If I change delegate:self to delegate:[RKRequestDelegate class] it does not crash, and gives response 200 (OK), but it did not callback a didLoadResponse!

Please help, and thanks.

UPDATE:

If I use [request sendSynchronously]; in RKRequestDelegate.m, response gets called.

Upvotes: 0

Views: 445

Answers (2)

anticyclope
anticyclope

Reputation: 1587

Make request a class variable.

EDIT: Here's some code, didn't check it in real life, but this way should work

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

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

Upvotes: 2

Paul de Lange
Paul de Lange

Reputation: 10633

Your crash is almost definitely because your delegate is being freed too early. If you don't feel like learning proper memory management, try moving to the blocks equivalent: [RKClient get:usingBlock:] and you might get lucky.

Upvotes: 1

Related Questions