Search code examples
c#user-interfacexamarinrefit

The UI is blocked during refresh ListView for xamarin form


Why is the UI blocked when I load data from the server using an async request as follows:

public ObservableCollection<VideoFile> VideoFiles { get; private set; } = new ObservableCollection<VideoFile>();

private void LoadVideoFiles(string categoryId = "*", int page = 1)
{
    _videoFiles = NotifyTask.Create(_videoManager.GetVideoFilesAsync(_videoFileSearchParam));
    _videoFiles.PropertyChanged -= VideoFilesPropertyChanged;
    _videoFiles.PropertyChanged += VideoFilesPropertyChanged;
}

private void VideoFilesPropertyChanged(object sender, PropertyChangedEventArgs propertyName)
{
    foreach (var videoFile in ((NotifyTask<List<VideoFile>>)sender).Result)
    {
        VideoFiles.Add(videoFile);
    }
}

Solution

  • UI is being blocked because calls you are making are synchronous, thus app will wait with the whole thread until this call ends. If you want to make UI responsive in the meantime you should make method asynchronous using async keyword. This way it will let method run, while all other UI elements will stay responsive. You can read more here