I am encountering a scenario where I need the user to be able to fire off an upload, but continue working on the app - and if possible, keep the user informed of upload progress wherever they are within the app, i.e. simple % value displayed on their current view.
For this, I imagined setting of the upload task in fire and forget style, where that task would make use of the MessagingCenter to inform the current subscribed view of the upload progress.
Can anybody point in the direction of the best way to achieve this? At the moment, it's looking like:
View where they are configuring the upload:
private async Task Upload()
{
Task.Factory.StartNew(() => _service.Upload(blah, foo));
//return to index so they can continue working, this should fire instantly
await Navigation.PopToRootAsync(true);
}
In the view they are return to:
private void SubscribeToUploadStatus()
{
//subscribe to listen for the upload status
MessagingCenter.Subscribe<UploadStatusModel>(this, "UploadStatus", (arg) =>
{
App.IsUploading = arg.IsUploading;
App.UploadProgress = arg.UploadProgress;
uploadProgressBar.IsVisible = arg.IsUploading;
uploadProgressBar.Progress = arg.UploadProgress;
});
}
This 'works' in a fashion, in that it will upload and perform the task in the background effectively, if I don't try to access the UI Thread, i.e. uploadProgress.IsVisible
I think I understand why, but I know this must be possible somehow, and maybe it's my implementation that's the problem.
Thanks