Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trigger an action to start after X milliseconds

I'm developing a Xamarin Forms mobile app, which has a page containing a SearchBar, a ListView, and Map control. The list view contains a list of addresses, which are reflected as pins on the map.

As the user types in the SearchBar, the ListView is automatically updated (through ViewModel binding). The ViewModel method that filters the datasource for the list looks something like this...

void FilterList()
{
    listDataSource = new ObservableCollection<location>(
        locationData.Where(l => l.Address.Contains(searchBar.Text))
    );

    // ***** Now, update the map pins using the data in listDataSource
}

I want to update the Map as the ListView is filtered, but not on EVERY keypress as that could happen multiple times a second. Essentially, I want a "rolling pause" in each FilterList event before the Map is updated. In pseudo-code...

    // ***** Now, update the map pins using the data in listDataSource
    if (a previously-requested map update is pending)
    {
        // Cancel the pending map update request
    }

    // Request a new map update in 1000ms using [listDataSource]

It's possible to do this using the Stopwatch class, which is available in Portable Class Libraries, but I suspect there's a cleaner/better way to accomplish this using Tasks.

Can anyone suggest a "cleverer" PCL-compatible way to do this?

like image 881
bosco Avatar asked Aug 07 '14 07:08

bosco


1 Answers

you can try :

await Task.Delay(2000);
like image 103
Rui Marinho Avatar answered Oct 25 '22 00:10

Rui Marinho