Search code examples
androidsqlitexamarinmvvmcross

MvvmCross with SQLite extremely slow


This is an Android application using Xamarin.

I have a simple search screen where the users select to filter based on state, city, interstate, etc (1 or more options). When state is changed, the other dropdowns change appropriately (based on what cities exist in that state, or interstates, etc). It's taking 2 seconds or more every time I change the state dropdown to update the other dropdowns, and meanwhile the UI is not responsive. I also notice the RaisePropertyChanged events don't actually occur until all code in the set method is completed. How can I speed up things to give a better user experience?

Here is the SQL call that is slow. I'm looking for a list of child objects based on a property of the parent.

        var parentIds = _connection.Table<Parent>().Where(x => x.State == state).Select(x => x.Id);
        return _connection.Table<Child>()
                          .Where(x => parentIds.Contains(x.ParentId))
                          .Select(x => x.ChildProperty)
                          .OrderBy(x => x)
                          .Distinct();

Solution

  • The solution was to change the query to the following:

            var parentIds = _connection.Table<Parent>().Where(x => x.State == state).Select(x => x.Id).ToList();
            var children = _connection.Table<Child>().ToList();
            return
                children.Where(x => parentIds.Contains(x.ParentId))
                        .Select(x => x.ChildProperty)
                        .OrderBy(x => x)
                        .Distinct();