PhoneGap: contact list selection with speed optimization

On Phonegap, I am trying to get a contact list from a phone. I only need a name and a phone number, it takes about 40 seconds to get the whole result. I am adding a plugin to config.xml. I only have 400 contacts on my phone. But when I warn about the length of the contact in index.html it says the list is 1351. I don't know where I am going wrong. I think some optimization is needed when getting the name and number from the phone.

advance Thanks ... :)

Config.xml

 <feature name="Contacts">
    <param name="android-package" value="org.apache.cordova.contacts.ContactManager" />
</feature>

      

index.html

        document.addEventListener("deviceready", onDeviceReady, false);
        function onDeviceReady() {
            var options = new ContactFindOptions();
            options.filter="";    
            options.multiple=true; 
            filter  = ["displayName", "phoneNumbers"];
            navigator.contacts.find(filter, onSuccess, onError, options);
        }

       var cSort = function(a, b) {
          aName = a.displayName ;
          bName = b.displayName ;
            return aName < bName ? -1 : (aName == bName ? 0 : 1);
            };

        function onSuccess(contacts) {              
            contacts = contacts.sort(cSort);
            alert("length " + contacts.length  );
            var i =0;       
            for (var i = 0; i < contacts.length; i++) 
            {
                console.log("Display Name = " + contacts[i].displayName);
                 if(contacts[i].displayName != null)
                 {
                    if( contacts[i].phoneNumbers == null )
                     continue;
                    else if(contacts[i].phoneNumbers.length)
                    {                           
                        for (var j=0; j<contacts[i].phoneNumbers.length; j++)
                         {
                          $('#contact_list').append('<li> Name:'+contacts[i].displayName+'</li>');
                          $('#contact_list').append('<li> Number:'+contacts[i].phoneNumbers[j].value+'</li><br><br>');
                        }
                        $('#contact_list').listview('refresh');
                    }
                 }
            }        
        }
        function onError(contactError) {
            alert('onError!');
        }
    </script>

      

+3


source to share


2 answers


Change your code:

            {                           
                for (var j=0; j<contacts[i].phoneNumbers.length; j++)
                 {
                  $('#contact_list').append('<li> Name:'+contacts[i].displayName+'</li>');
                  $('#contact_list').append('<li> Number:'+contacts[i].phoneNumbers[j].value+'</li><br><br>');
                }
                $('#contact_list').listview('refresh');
            }

      



To:

                {
                    var finalList = '';
                    listEntryPoint = $('#contact_list');
                    for (var j=0; j<contacts[i].phoneNumbers.length; j++)
                     {
                        finalList += '<li> Name:'+contacts[i].displayName+'</li>' + '<li> Number:'+contacts[i].phoneNumbers[j].value+'</li><br><br>';
                    }
                    listEntryPoint.append(finalList);
                    listEntryPoint.listview('refresh');
                }

      

0


source


@BINIL S, you have a very expensive Jquery call:

$('#contact_list').append(...);

      

Change to:

listEntryPoint = $('#contact_list');

      

move this outside of the loop and assign it to a variable that should help. You also can NOT insert new records one at a time. You can make one big list before pasting into HTML. Like this,



finalList += '<li> Name:'+contacts[i].displayName+'</li>' + '<li> Number:'+contacts[i].phoneNumbers[j].value+'</li><br><br>';

      

After the end of the cycle

listEntryPoint.append(finalList);

      

This should help - Jesse

0


source







All Articles