MonoTouch Load image in background

Posted by user1058951 on Stack Overflow See other posts from Stack Overflow or by user1058951
Published on 2012-10-15T02:18:50Z Indexed on 2012/10/15 9:37 UTC
Read the original article Hit count: 161

Filed under:
|
|
|

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?

© Stack Overflow or respective owner

Related posts about ios

Related posts about multithreading