Search code examples
c#asp.net-web-apiasp.net-web-api2cordova-plugins

Best way to comapare Phone contacts with Server


I am using Corodva Contacts Plugin to fetch the local phone contacts its working great,Now i need to compare it with database,I am not using local db, I am using sql server 2012 and I have written some back-end code with WEBAPI.Its taking lot of time to compare. I need some alternative solution to it. Kindly suggest below is my code.

         //Javascript///

  var phoneNumberCollection = new Array();

  function showContacts() {

var options = new ContactFindOptions();
  options.filter = "";
  options.multiple = true;
  options.desiredFields = [navigator.contacts.fieldType.id,navigator.contacts.fieldType.displayName,  navigator.contacts.fieldType.name, navigator.contacts.fieldType.phoneNumbers];
options.hasPhoneNumber = true;
   var fields = [navigator.contacts.fieldType.displayName, navigator.contacts.fieldType.name];
   navigator.contacts.find(fields, onSuccess, onError, options);
}

 function onSuccess(contacts) 
  {
     // here i have all contacts and i am pushing each number into       "phoneNumberCollection" array//
  }

 function onError(err)
 { 
 }

  self.GetContactsData = function () {
    self.PhoneNumberCollection = phoneNumberCollection;
    jQuery.support.cors = true;
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: 'application/json; charset=utf-8',
        data: JSON.stringify({mobilecollection:  self.PhoneNumberCollection,}),
        url: Url + 'api/xxxxxx/xxxxxxxx',
        success: function (data) {
            self.items($.map(data, function (item) {
                return new ContactsModel(item);
            }));
        },
        error: function (err, type, httpStatus) {
         }
    })

}

     //Web API
  [HttpPost]
    public  IHttpActionResult GetContacts(JObject jsonData)
    {
        try
        {
            if (jsonData != null)
            {
                dynamic json = jsonData;

                string[] mobilenumberCollection = json.mobilecollection.ToObject<string[]>();
    //here i am getting Mobile collection and i am comparing each number with DB. i need some alternative sugggestion for this
   var getContacts = CBFriends.getAllcontacts(mobilenumberCollection, deviceUID);
                if (getAllContacts != null)
                {
                    return Ok(getAllContacts);
                }
                else { return NotFound(); }

            }
            else {
                return BadRequest();}
           }
        catch (Exception)
        { }      
       }

Solution

  • Check IN operator in sql server.

    You can send all phone numbers in one query and get ones that not exists in db.

    Check this answer, to see how to achieve this in c#.