I have a class that uses AsiHTTPRequest. I want to make a method like this:
-(NSData*)downloadImageFrom: (NSString*)urlString;
{
// Set reponse data to nil for this request in the dictionary
NSMutableData *responseData = [[NSMutableData alloc] init];
[responseDataForUrl setValue:responseData forKey:urlString];
// Make the request
NSURL *url = [NSURL URLWithString:urlString];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[responseDataForUrl setValue:responseData forKey:[request url]];
[request setDelegate:self];
[request startAsynchronous];
// Wait until request is finished (???? Polling ?????)
// while(responsedata = nil) {
// do nothing
// } BAD SOLUTION
return responseData;
}
After that. A delegate method is called when the responseData is ready. Is there a better solution to continue, than doing polling in the variable responseData?
Your delegate doesn't have to be a separate class.
ReplyDelete- (void)someMethod:(NSUrl *)url
{
ASIHTTPRequest *req = [ASIHTTPRequest requestWithUrl:url];
[req setDelegate:self];
//configure the request
[req startAsynchronous];
}
- (void)requestDone:(ASIHTTPRequest *)request
{
NSString *response = [request responseString];
//do whatever with the response
}
So your method someMethod: fires the request and returns void. When the request is finished, your ASIHTTPRequest fires the requestDone: method on its delegate, which is this same object. In that method, you do whatever--set an ivar or a named property, process incoming data and populate a UITableVew, whatever.
Note that ASIHTTPRequest is now deprecated, and its author recommends using something else instead. AFNetworking seems a popular choice, but I haven't started a new project recently so I haven't picked one yet myself.
I use ASIHttpRequest for most of my web-service calls, but in your case (getting Image data async), I use blocks with GCD. I have a class called WebImageOperations and in that class I have a class method:
ReplyDeleteWebImageOperations.h:
+ (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage;
WebImageOperations.m:
+ (void)processImageDataWithURLString:(NSString *)urlString andBlock:(void (^)(NSData *imageData))processImage
{
NSURL *url = [NSURL URLWithString:urlString];
dispatch_queue_t callerQueue = dispatch_get_current_queue();
dispatch_queue_t downloadQueue = dispatch_queue_create("com.achcentral.processimagedataqueue", NULL);
dispatch_async(downloadQueue, ^{
NSData * imageData = [NSData dataWithContentsOfURL:url];
dispatch_async(callerQueue, ^{
processImage(imageData);
});
});
dispatch_release(downloadQueue);
}
Then to call it, use this:
[WebImageOperations processImageDataWithURLString:@"MyURLForPicture" andBlock:^(NSData *imageData) {
if (self.view.window) {
UIImage *image = [UIImage imageWithData:imageData];
self.myImageView.image = image;
}
}];
You definitely shouldn't do Polling!
ReplyDeleteThe delegate of the ASIHHTPRequest that you set to be self will call a method (see the ASIHTTPRequest documentation for details of that delegate method) to notify you when it is finished. In that delegate method, call whatever other code you want to do then. Don't bother with returning the image - it's all asynchronous.