0

Edit: Sorry - now that I've understood the problem a bit better, I think my problem lies elsewhere

I have 2 asynchronus requests.

The first is this:

public void DownloadWebData(Uri apiUrl)
{
    WebClient client = new WebClient();
    client.DownloadDataCompleted += DownloadDataCompleted;
    client.DownloadDataAsync(apiUrl);
}

public void DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    string result = System.Text.Encoding.UTF8.GetString (e.Result);
    Uri downloadLink = (GetUri(result));
}

Basically it makes a simple url based API request to a remote webserver which returns some basic textual data over http. GetUri() just parses that data to extract an address from the data for an image to download.

I'm then using imageLoader in monotouch.dialog to download the image. All code is in the same class.

Edit: added the imageLoader code (I left the Console lines in because they serve reasonably well as comments).

public void downloadImage (Uri imageUri) 
{
    var tmp_img = ImageLoader.DefaultRequestImage (imageUri, this);

    if (tmp_img != null)
    {
        adView.Image = tmp_img;
        Console.WriteLine ("Image already cached, displaying");
    }
    else
    {
        adView.Image = UIImage.FromFile ("Images/downloading.jpg");
        Console.WriteLine ("Image not cached.  Using placeholder.");
    }
}

public void UpdatedImage (System.Uri uri) 
{
    adView.Image = ImageLoader.DefaultRequestImage(uri, this);
}
Nande
  • 637
  • 1
  • 7
  • 18
  • can you post exception details? is it `NullReferenceException` or `ArgumentNullException`? – Sriram Sakthivel Jul 14 '13 at 08:52
  • I think I've identified the problem through some code reworking. I think it's because the code is trying to update the UI, which requires the main UI thread, but this is run as an asynchronus thread so as not to block the UI. The *real* problem (if I understand it correctly) is how to retrieve the image and then pass control back to the main UI thread in order to display it - that's where I'm stumbling. I'm calling a function back in the main viewcontroller to update the a UIImageview.Image, but how do I force this to happen back on the main UI thread? – Nande Jul 14 '13 at 09:20
  • I think I'm going to have to make a different question, as it's entirely different to the one I thought. – Nande Jul 14 '13 at 09:28

3 Answers3

0

You missed to check if e.Result actually contains something. The download might as well have failed and e.Result is null. Add some basic error handling to your code.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • It's a good suggestion, but in the real code I do check and it does contain something - I also write it to the console, so it's definitely not null. I just stripped out most of the error handling in the examples to try and keep the code brief and give the core functionality - sorry. – Nande Jul 14 '13 at 08:48
0

if you are using DownloadWebData inside a for loop, it will be better you generate seperate functions for DownloadDataCompleted event. You can use anonymous function inside DownloadWebData().

client.DownloadDataCompleted +=(s,e)=>{
    string result = System.Text.Encoding.UTF8.GetString (e.Result);
    Uri downloadLink = (GetUri(result));
};
Deepak Mishra
  • 2,984
  • 1
  • 26
  • 32
0

After realizing I was asking the wrong question, I finally figured it out here:

Hand back control to main UI thread to update UI after asynchronus image download

Community
  • 1
  • 1
Nande
  • 637
  • 1
  • 7
  • 18