Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS RestKit how to send a request with a callback object (userData) set?

I'm trying to create a RestKit request to load an image from a web service and add it to a button as a background image. To do so asynchronously, I'm trying to add the button as a userData to the RKRequest object.

What I'm not sure of is how to send a fully configured RKRequest, I tried setting the delegate, calling prepareURLRequest and sendAsynchronously . The method that I expect to be called back does not get called.

- (void)didFinishLoad:(RKResponse*)response

To check that my request is properly configured, I sent it via a request queue, and that works.

What is the proper way to send fully configured requests with userData object using RestKit?

- (void)sendRequestForTrackerUserGravatar:(CoreDataButton*)coreDataButton {

/*This works, but does not allow the user data object to be set, meaning I don't know which button's image was loaded.

    NSMutableDictionary* paramsDictionary = [[NSMutableDictionary alloc] init];   
    [paramsDictionary setValue:@"identicon" forKey:@"d"];

[[RKClient sharedClient] get:[self gravatarMD5HashWithUserID:trackerUser.user_id.intValue] queryParams:paramsDictionary delegate:self];
*/

    //single request for a resource, includes a dictionary of parameters to be appended to the URL
    //    [paramsDictionary setValue:@"retro" forKey:@"d"];
    User* user = (User*)coreDataButton.managedObject;


    NSString* md5Hash = [self gravatarMD5HashWithUserID:user.user_id.intValue];
    NSString* urlString = [NSString stringWithFormat:@"%@%@?d=identicon",kGravatarWebsite,md5Hash];
    RKRequest* request = [[RKRequest alloc] initWithURL:[NSURL URLWithString:urlString] delegate:self];
    request.userData = coreDataButton;
    request.delegate = self;   
    request.cachePolicy = RKRequestCachePolicyEnabled;

//this works    
//[[RKRequestQueue sharedQueue] addRequest:request];


//this does not seem to call back the delegate method    
    [request prepareURLRequest];
    [request sendAsynchronously];

    }

//request callback, does not get called
- (void)didFinishLoad:(RKResponse*)response
{
    [self processResponse:response];
}

//queue callback, DOES get called
-(void)requestQueue:(RKRequestQueue *)queue didLoadResponse:(RKResponse *)response
{
    [self processResponse:response];
}
like image 646
Alex Stone Avatar asked Dec 04 '25 09:12

Alex Stone


1 Answers

This line:

[[RKClient sharedClient] get:[self gravatarMD5HashWithUserID:trackerUser.user_id.intValue] queryParams:paramsDictionary delegate:self];

actually returns the request that is created and then dispatched, so you can do:

RKRequest* r = [[RKClient sharedClient] get:[self gravatarMD5HashWithUserID:trackerUser.user_id.intValue] queryParams:paramsDictionary delegate:self];
[r setUserData:buttonReference];

and then catch the success with RKRequest delegate method:

- (void)request:(RKRequest *)request didLoadResponse:(RKResponse *)response {
    // get button like this
    UIButton* b = [request userData];
    // process the response and get image
    UIImage* i = [UIImage imageWithData:[self giveMeDataFromResponseNOW:response]]; //or however you handle responses
}
like image 101
lawicko Avatar answered Dec 05 '25 23:12

lawicko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!