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

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

CCNA 3 Final Exam => latest version

1 . Which security protocol or measure would provide the greatest protection for a wireless LAN? WPA2 cloaking SSIDs shared WEP key MAC address filtering   2 . Refer to the exhibit. All trunk links are operational and all VLANs are allowed on all trunk links. An ARP request is sent by computer 5. Which device or devices will receive this message? only computer 4 computer 3 and RTR-A computer 4 and RTR-A computer 1, computer 2, computer 4, and RTR-A computer 1, computer 2, computer 3, computer 4, and RTR-A all of the computers and the router   3 . Refer to the exhibit. Hosts A and B, connected to hub HB1, attempt to transmit a frame at the same time but a collision occurs. Which hosts will receive the collision jamming signal? only hosts A and B only hosts A, B, and C only hosts A, B, C, and D only hosts A, B, C, and E   4 . Refer to the exhibit. Router RA receives a packet with a source address of 192.168.1.65 and a destination address of 192.168.1.161...