IOS RestKit, how to send a request using a callback object (userData)?
I am trying to create a RestKit request to download an image from a web service and add it to a button as a background image . To do this asynchronously, I am trying to add a button as userData to the RKRequest object.
I'm not sure how to send a fully configured RKRequest, I tried to set delegate
by calling prepareURLRequest
and sendAsynchronously
. The method that I expect to be called is not called.
- (void)didFinishLoad:(RKResponse*)response
To test if my request was configured correctly, I sent it through the request queue and it works.
What is the correct way to send fully customized requests using the userData object using RestKit?
- (void)sendRequestForTrackerUserGravatar:(CoreDataButton*)coreDataButton {
/ * This works, but doesn't allow setting the user data object, i.e. I don't know which button 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];
}
source to share
This line:
[[RKClient sharedClient] get:[self gravatarMD5HashWithUserID:trackerUser.user_id.intValue] queryParams:paramsDictionary delegate:self];
actually returns the created and then submitted request, 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 success with the RKRequest delegation 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
}
source to share