radzenhq / radzen-blazor

Radzen Blazor is a set of 90+ free native Blazor UI components packed with DataGrid, Scheduler, Charts and robust theming including Material design and FluentUI.
https://www.radzen.com
MIT License
3.57k stars 798 forks source link

RadzenDataGrid: OnPageSizeChanged and ReloadOnFirstRender redundantly reload #1019

Closed evandixon closed 1 year ago

evandixon commented 1 year ago

Describe the bug On the first render of a data grid with both a LoadData delegate and a manually specified PageSize, the LoadData delegate is called twice, once by PagedDataBoundComponent<>.OnPageSizeChanged and once by PagedDataBoundComponent<>.ReloadOnFirstRender.

To Reproduce Steps to reproduce the behavior:

  1. Create a RadzenDataGrid
  2. Define both the LoadData and the PageSize parameters in any order
  3. Observe the LoadData implementation being called twice on the first page load.

Expected behavior The initial set of PageSize doesn't trigger a reload when ReloadOnFirstRender is going to trigger a reload

enchev commented 1 year ago

Both PageSize and LoadData are used in this demo however LoadData will be called only once: https://blazor.radzen.com/datagrid-loaddata

evandixon commented 1 year ago

On that demo, I added Console.WriteLine("Reload called. Stack trace: " + Environment.StackTrace.Replace("\n", "\\n")); to line 102 at the very top of LoadData, and looking in the console shows that LoadData is indeed being called twice, once from ReloadOnFirstRender and once from PageSizeChanged. It's easier to see when LoadData makes a network call since you can see two distinct network requests.

enchev commented 1 year ago

Hey @evandixon,

I've used the debugger to check the same: loaddata-pagesize

evandixon commented 1 year ago

@enchev Turns out there's a race condition with the reload caused by OnPageSizeChanged not being awaited by the setter of PageSize. If the reload works quickly there, ReloadOnFirstRender will see that there's data and not reload. Adding a await Task.Delay(1000); to the start of LoadData results in ReloadOnFirstRender loading before OnPageSizeChanged completes.