Skip to main content

Return Value of method using AsiHTTPRequest



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?


Comments

  1. Your delegate doesn't have to be a separate class.

    - (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.

    ReplyDelete
  2. 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:

    WebImageOperations.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;
    }
    }];

    ReplyDelete
  3. You definitely shouldn't do Polling!

    The 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...