Hey guys!
I already put many hours into researching the following problem:
I have a ContentPage in my PCL-project which consists of a ProgressBar and a Label. Both should display the progress of a download running in a background task. Therefore I'm using the MessageCenter to subscribe to a channel on which I publish the current download's progress (DownloadProgressMessage
).
Additionally, I have a ViewModel which holds two Properties (double DownloadProgress, string DownloadText) which are binded to the ProgressBar + Label. When I receive a new DownloadProgressMessage I update the Properties accordingly within the ViewModel so the displayed values get updated. The messaged are sent from the DidWriteData()
method in my NSUrlSessionDownloadDelegate
. When I run the app from VS (Debug OR Release) everything works just fine. The download starts, progressbar and label get updated. When I switch to another app and get back into mine, all values got updated accordingly. Now comes the strange part:
When I force stop the app and start it again from the homescreen of the iPad, switching to another app and coming back results in an UI-freeze. The values are stuck on the state where I left the app, but the download continues in the background. I can't debug the problem because it only appears if the app is not "connected" to VS.
Property example (using GalaSoft.MvvmLight):
private double _progressValue; public double ProgressValue { get => _progressValue; set { Set(ref _progressValue, value); } }
Message subscribe in ViewModel:
MessagingCenter.Subscribe<DownloadProgressMessage>(this, "DownloadProgressMessage", message => { ProgressValue = message.Percentage; ProgressText = "downloading".Translate() + " " + message.Percentage + "% " + "percent_of".Translate() + " " + ((message.TotalBytesExpectedToWrite / 1024f) / 1024f).ToString("#0.##") + " MB"; });
DidWriteData from the NSUrlSessionDownloadDelegate:
public override void DidWriteData( NSUrlSession session, NSUrlSessionDownloadTask downloadTask, long bytesWritten, long totalBytesWritten, long totalBytesExpectedToWrite) { // Debug.WriteLine($"{requestUri}:{percentage}%"); var message = new DownloadProgressMessage() { BytesWritten = (long)totalBytesWritten, TotalBytesExpectedToWrite = (long)totalBytesExpectedToWrite, Percentage = totalBytesWritten * 100 / totalBytesExpectedToWrite }; MessagingCenter.Send(message, "DownloadProgressMessage"); }
Does anyone have an idea why this is happening and more important, why it only happens when the app is not "connected" to VS?
Thanks in advance!