0

i'm working on application that getting the duplicate contacts that has same phone number . my problem is that the regular foreach is slow with large amount of contacts , i know also that the there's away to do it using predicate . but i couldn't find any sample with monotouch.

2 Answers2

0

I don't know about ABAddressBook, but if you use the Xamarin Mobile APi, then you can use predicates as shown in:

var abook = new AddressBook();
abook.RequestPermissions().ContinueWith (t =>
{
    if (!t.Result)
        return; // Permission denied

    var builder = new StringBuilder();

    // Full LINQ support
    foreach (Contact c in abook.Where (c => c.FirstName == "Eric" && c.Phones.Any()))
    {
        builder.AppendLine (c.DisplayName);
        foreach (Phone p in c.Phones)
            builder.AppendLine (String.Format ("{0}: {1}", p.Label, p.Number));

        builder.AppendLine();
    }

    contacts.Text = builder.ToString(); // Update UI

}, TaskScheduler.FromCurrentSynchronizationContext()); // Ensure we're on the UI Thread

from http://betaapi.xamarin.com/?link=T%3aXamarin.Contacts.AddressBook

Stuart
  • 66,722
  • 7
  • 114
  • 165
  • To achieve my goal using this sample , it would be a nested foreach and still the performance would be slow .. if i want to get duplicate phones i need to loop contacts and get the phone of each one and then loop through them to get the duplicates. – Ahmed Mohamed Anas Feb 05 '13 at 20:46
0

You can do it in O(N) time. This answer uses one loop to identify duplicates in an array: https://stackoverflow.com/a/12948182/1441667 . Try to combine Stuarts answer with this approach.

Community
  • 1
  • 1
Norbert Szenasi
  • 993
  • 10
  • 24