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 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...