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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex