DataSoruce Filter Predicate very slow

Hi 

I am using the following function to filter items being displayed in my SfListview:

private bool FilterContacts(object obj)
        {
            if (searchBar == null || searchBar.Text == null ||   ZZZ.Instance.SearchPhrase.Length < 4)
                return true;

            var contacts = obj as IUnifiedOrder;

            

            if (ZZZ.ToJsonIUnified(contacts).ToLower().Contains(searchBar?.Text?.ToLower()?.Trim()))
                return true;
                else
                    return false;
      
        }


I am converting the object to a json string to search all of its fields and as it is using Reflection by default, I have written a custom serializer but still, it is very slow!

And while it is doing that, it is blocking the UI thread. I was wondering if there is a way so that I can await this search using Task or run it on a background thread?!

Thanks!


5 Replies

CS Chandrasekar Sampathkumar Syncfusion Team February 25, 2020 09:55 AM UTC

Hi Amir, 
 
Thank you for using Syncfusion products. 
 
We have checked the reported query from our end. We would like to suggest you to use FilterContacts method execution in Device.BeginInvokeOnMainThread() like in the following code snippet, 
 
 
private bool FilterContacts(object obj) 
{ 
    Device.BeginInvokeOnMainThread(()=> 
    { 
        if (searchBar == null || searchBar.Text == null || ZZZ.Instance.SearchPhrase.Length < 4) 
            return true; 
 
        var contacts = obj as IUnifiedOrder; 
 
        if (ZZZ.ToJsonIUnified(contacts).ToLower().Contains(searchBar?.Text?.ToLower()?.Trim())) 
            return true; 
        else 
            return false; 
    }); 
} 
 
 
We hope this helps. Please let us know if you need any further assistance. 
 
Regards, 
Chandrasekar Sampathkumar 
 



AH Amir H February 25, 2020 09:59 AM UTC

Hi,

Sorry but your answer is wrong!

First of all, you are asking me to surround my code with Device.BeginInvokeOnMainThread so how can it return a boolean value (it won't even compile!)

Secondly, the code I have does run on the UI thread and I want it NOT to run on UI thread but instead run on background thread so that the UI is not frozen. 


CS Chandrasekar Sampathkumar Syncfusion Team February 25, 2020 06:03 PM UTC

Hi Amir, 
 
Thank you for the update. 
 
We regret to inform that UI gets updated only after the ListView ItemsSource is updated. So you cannot update using the background thread, it is the behaviour of ListView. We suggest you to maintain a separate collection to hold the filtered entries and after filtering, assign it to ListView ItemSource like in the following code snippet, 
 
Code snippet C# : Filtering in SearchBar TextChanged event 
 
 
private void SearchBar_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    searchBar = (sender as SearchBar); 
    filterSource = new ObservableCollection<TaskInfo>(); 
 
    foreach(var item in sortingFilteringViewModel.Items) 
    { 
        if (searchBar == null || searchBar.Text == null) 
            return; 
 
        if (item.Title.ToLower().Contains(searchBar.Text.ToLower()) 
                || item.Description.ToLower().Contains(searchBar.Text.ToLower())) 
        { 
            filterSource.Add(item); 
        } 
    } 
    ListView.ItemsSource = filterSource; 
} 
 
 
  
Sample Link : SfListViewSample 
  
Please check the attached sample and let us know if you need further assistance? If yes, please modify our sample and revert us back or share the issue replicating video which would be helpful for us to check on it and provide the solution at the earliest.  
 
Meanwhile, we will validate and check the possibilities with our source regarding your requirement and update you further details on or before Feb 28, 2020.  
 
Regards, 
Chandrasekar Sampathkumar 



CS Chandrasekar Sampathkumar Syncfusion Team February 29, 2020 03:36 AM UTC

Hi Amir, 
 
Thank you for your patience. 
 
We like to inform you that ListView updates the UI synchronously, so you can run on background thread by using Task to filter records. Meanwhile, we are validating and provide you the best possible sample level solution on or before Mar 4, 2020. 
 
Regards, 
Chandrasekar Sampathkumar 



LN Lakshmi Natarajan Syncfusion Team March 4, 2020 05:39 PM UTC

Hi Amir, 
 
We regret for the inconvenience caused. 
 
We have analyzed the reported query “DataSource Filter predicate is slow” from our end. On further analysis, we would like to let you know that as per the implementation, we will update the UI synchronously while searching the items in ListView. For each call to the FilterContact method, the UI will be updated with the search result. This is the behavior of filter in ListView and we have ensured the same in our source. Also, this process cannot be achieved using background thread or separate task since, every search updates the UI and Layout will be updated in the main thread.  
 
So, We suggest you to maintain a separate collection to hold the filtered entries and assign it to ListView ItemSource after filtering as we have mentioned in our previous update. 
 
Please let us know if you need any further assistance. 
 
Regards, 
Lakshmi Natarajan 


Loader.
Up arrow icon