Getting the count of contacts

635 views
Skip to first unread message

srbrand

unread,
Jan 19, 2011, 1:25:26 AM1/19/11
to phonegap
I am trying to get a count of contact. But the way the API works right
now is the on the success of contactCount it calls the
onCountSuccess() function which has the value.

Way it works right now:
navigator.contacts.contactsCount(onCountSuccess,onCountError);

function onCountSuccess(cCount){
console.log("Count"+ cCount);
}

function onCountError(err){
alert("error getting contacts count " + err);
}

I want something like:
var contactCount = navigator.contacts.contactsCount();

Is this possible?

Jesse

unread,
Jan 19, 2011, 3:03:22 AM1/19/11
to srbrand, phonegap
No, it is async, the value is not known until you ask for it.


Sent from my iPad

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com

steve dunkley

unread,
Jan 19, 2011, 3:37:17 AM1/19/11
to srbrand, phonegap
A work-around would be to use the onCountSuccess to assign the value to a global variable, something like this:


var contactCount;

function onCountSuccess(cCount){
countactCount = cCount;

}

function onCountError(err){
alert("error getting contacts count " + err);
}

function getContactCount() {
navigator.contacts.contactsCount(onCountSuccess,onCountError);
}


then you'd just need to call getCountactCount, and the contactCount global should be populated with the result.

Steve



Simon MacDonald

unread,
Jan 19, 2011, 8:04:11 AM1/19/11
to phonegap
Here is some code that will give you the total number of contacts in
the phone. That number won't be known until the onSuccess method of
the find command is called. This should work on iPhone, Android and
BB.

function countContacts()
{
var obj = new ContactFindOptions();
obj.filter="";
obj.multiple=true;
obj.limit=1000000;
navigator.service.contacts.find(["displayName"], onSuccess, fail, obj);
}

function onSuccess(contacts)
{
alert(contacts.length);
}

Simon Mac Donald
http://hi.im/simonmacdonald

srbrand

unread,
Jan 20, 2011, 1:43:40 AM1/20/11
to phonegap
The global variable way and the way suggested by using
ContactFindOptions() won't help. The reason is explained below...

let's say the global variable contactCount is initialized to 0;

var beforeContactCount = getContactCount();
console.log("Before "+ beforeContactCount );
createANewContact();// Call to create a new contact
var afterContactCount = getContactCount();
console.log("Before "+ afterContactCount );

The problem is the above five lines of code would have run and the
onContactSuccess() or onSuccess() would be called after to set the
value of contactCount variable. In that case I will be getting a value
0 for beforeContactCount & afterContactCount.

On Jan 19, 5:04 am, Simon MacDonald <simon.macdon...@gmail.com> wrote:
> Here is some code that will give you the total number of contacts in
> the phone.  That number won't be known until the onSuccess method of
> the find command is called.  This should work on iPhone, Android and
> BB.
>
>         function countContacts()
>         {
>                 var obj = new ContactFindOptions();
>                 obj.filter="";
>                 obj.multiple=true;
>                 obj.limit=1000000;
>                 navigator.service.contacts.find(["displayName"], onSuccess, fail, obj);
>         }
>
>         function onSuccess(contacts)
>         {
>                 alert(contacts.length);
>         }
>
> Simon Mac Donaldhttp://hi.im/simonmacdonald

Jesse

unread,
Jan 20, 2011, 1:53:32 AM1/20/11
to srbrand, phonegap
The bottom line is that the call is async. Until the callback happens, you will not have the value.

Sent from my iPhone

Reply all
Reply to author
Forward
0 new messages