I am using following code to load contacts using ContactStore. My requirement is to search contacts in ContactStore when key is pressed in a textbox. Below is the code in my View Model.
private async void LoadAllContacts(string searchQuery)
{
ContactStore contactStore = await ContactManager.RequestStoreAsync();
IReadOnlyList<Contact> contacts = null;
// Find all contacts
contacts = await contactStore.FindContactsAsync(searchQuery);
foreach (var item in contacts)
{
if (!string.IsNullOrEmpty(item.FirstName) && !string.IsNullOrEmpty(item.LastName))
{
var contact = new Member
{
FirstName = item.FirstName,
LastName = item.LastName,
FullName = item.DisplayName, //item.HonorificNamePrefix ?? "" + item.FirstName + item.MiddleName ?? "" + item.LastName,
Bio = item.Notes
};
if (item.DataSuppliers != null)
{
foreach (var dataSupplier in item.DataSuppliers)
{
switch (dataSupplier.ToLower())
{
case "facebook":
break;
case "hotmail2":
case "hotmail":
break;
}
}
}
if (item.Thumbnail != null)
{
var thumnailStream = await item.Thumbnail.OpenReadAsync();
BitmapImage thumbImage = new BitmapImage();
thumbImage.SetSource(thumnailStream);
contact.ImageSource = thumbImage;
}
this.Insert(0, contact);
}
}
}
The contacts are loading perfectly in my listview but the problem is that loading contacts from contact store is an extensive task and when user I press text in the textbox quickly then application throws exception.
My question is how can I load contacts efficiently? Means if User pressed 'a' and I call this method and quickly user pressed 'c' so if the results are loaded for 'a' then application should cancel continuing with previous operation and load 'ac' related contacts.
Thanks.