MonoTouch Load image in background
- by user1058951
I am having a problem trying to load an image and display it using System.Threading.Task
My Code is as follows
Task DownloadTask { get; set; }
public string Instrument { get; set; }
public PriceChartViewController(string Instrument) {
this.Instrument = Instrument;
DownloadTask = Task.Factory.StartNew(() => { });
}
private void LoadChart(ChartType chartType) {
NSData data = new NSData();
DownloadTask = DownloadTask.ContinueWith(prevTask => {
try {
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
NSUrl nsUrl = new NSUrl(chartType.Uri(Instrument));
data = NSData.FromUrl(nsUrl);
}
finally {
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
}
});
DownloadTask = DownloadTask.ContinueWith(t => {
UIImage image = new UIImage(data);
chartImageView = new UIImageView(image);
chartImageView.ContentScaleFactor = 2f;
View.AddSubview(chartImageView);
this.Title = chartType.Title;
}, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext());
}
The second Continue with does not seem to be being called?
Initially my code looked like the following without the background processing and it worked perfectly.
private void oldLoadChart(ChartType chartType) {
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;
NSUrl nsUrl = new NSUrl(chartType.Uri(Instrument));
NSData data = NSData.FromUrl(nsUrl);
UIImage image = new UIImage(data);
chartImageView = new UIImageView(image);
chartImageView.ContentScaleFactor = 2f;
View.AddSubview(chartImageView);
this.Title = chartType.Title;
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
}
Does anyone know what I am doing wrong?