Binding big database with observablecollection

quanlich

New member
Sep 11, 2012
41
0
0
Visit site
Hi,

I'm currently writing a dictionary app using databound app template. My database is a sqlite database with more than 300.000 rows. Here is my code to store loaded database.

Code:
 public class Word
    {
        [PrimaryKey]
        public int Id { get; set; }
        
        public string WordString { get; set; }

        public string Content { get; set; }
    }

 public class WordList
    {
        public ObservableCollection<Word> Items { get; set; }

        public WordList()
        {
            Items = new ObservableCollection<Word>();
        }
    }

public class ViewModel {

//....
       public async void LoadData()
        {
            var query =  DB.Table<Word>();
            
            var result = await query.ToListAsync();
            

            foreach (Word w in result)
            {
                WList.Items.Add( new Word()
                {
                    Id = w.Id,
                    WordString = w.WordString,
                    Content = w.Content
                });
            }

            IsDataLoaded = true;
        }

}

And I use a TextBlock to display wordString and a phoneTextBox to search words.

Code:
 <toolkit:PhoneTextBox Hint="Search"
                                  TextChanged="SearchBox_TextChanged"
                                  Name="SearchBox"
                                  VerticalAlignment="Top" />

.....
<TextBlock Text="{Binding WordString}"
                                       FontSize="30"
                                       TextWrapping="Wrap"
                                       VerticalAlignment="Center"
                                       Margin="20,0,0,0"
                                       Foreground="Black"
                                       FontWeight="Bold"
                                       Grid.Row="0" />

And here is the code for searching:

Code:
....
 private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)        {
IList<Word> results = App.ViewModel.WList.Items
            .Where(tb =>tb.WordString.StartWith(SearchBox.Text))
            .ToList();

    
     ListBox1.ItemsSource = results;}

The problem is when I do a search, it takes to long to response.
Could you please give me a hint to accelerate my search? Could I create an indexer on ObservableCollection?

Thanks in advance.
 

Catholic Tech Geek

New member
Feb 2, 2012
130
0
0
Visit site
Instead of using StartsWiith(),you could try using the overloaded IndexOf(string, startindex, count) for String. This is just an idea.

example: exstring.IndexOf(SearchBox.Text, 0, SearchBox.Text.Length);
 

quanlich

New member
Sep 11, 2012
41
0
0
Visit site
Thank Catholic but it doesn't help much. It even made the app crash.

I tried to change observableCollection to other collections like Dictionary, List, HashShet... but I don't know why they do not display data on ListBox? Only ObservableCollection which functions!!
 

Members online

Forum statistics

Threads
323,324
Messages
2,243,635
Members
428,061
Latest member
cagkles124