Search code examples
objective-cios5grand-central-dispatch

iOS 5 - Capture the return value from a method being called asynchronously


I have a method, fetchFromWeb, that returns an object. I want to call this method asynchronously from GCD. Can someone explain how I would capture the return value from a method being called asynchronously?

Or, should I set the object value and then set up some sort of notification and not explicitly get the return value from this method at all?

Here is what I have and thanks!

        dispatch_async(kBGQueue, ^{


            NSData *data = [NSData dataWithContentsOfURL:kURL];
            [self performSelectorOnMainThread:@selector(fetchFromWeb:) 
                                   withObject:data 
                                waitUntilDone:YES];

            //Need to get the return value from method fetchFromWeb in selector....

        });

Solution

  • dataWithContentsOfURL is only for file URLs, not Web URLs. Its not asynchronous. Even if you send it off to its own thread using GCD its not the correct way to do it.

    If you need to go out to the web, you need to use NSURLConnection.

    Use + (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue )queue completionHandler:(void (^)(NSURLResponse, NSData*, NSError*))handler

    You were on the right track initially -- you don't want a return value from the method. Let the completion handler block set a property (ivar) with the data you received, and, as you suggested, use a notification at the end of the block to alert you that its done. That notification can fire a method to access the property and you can get at your data object.