We have an ionic app using cordova contacts plugin wrapped in ngcordova. We load whole address book to use in the app for use in the app.
Some users are reporting that that have only 100 contacts and contacts never load.
I am running into this issue except that on Galaxy s1, 4.2.2 it takes about 24 seconds to load 700 contacts. However some of my users are not able to load any contacts it just hangs and never hides loading indicator.
I was wondering if anyone saw a weird contact character or missing field that would cause the process of loading contacts to brake? Also i have an alert in error callback alerting error code, but it is never called...
Thanks a ton!
Here is my code:
function onSuccess(results) {
// set a flag that contacts loaded successfully
$scope.contactsMonitor.contactsLoadSuccess = true;
var sortResults = [];
angular.forEach(results, function (objContact, key) {
if (objContact.phoneNumbers) {
angular.forEach(objContact.phoneNumbers, function(phone, key){
phone.checked = false;
//console.log(objContact.id + ' : ' + phone.value + ' is checked: ' + phone.checked);
});
this.push({
id: objContact.id,
first: objContact.name.givenName !== null ? objContact.name.givenName : '',
last: objContact.name.familyName !== null ? objContact.name.familyName : '',
email: objContact.emails !== null ? objContact.emails[0].value : '',
phones: objContact.phoneNumbers !== null ? objContact.phoneNumbers : []
});
}
}, sortResults);
$scope.results = sortResults;
$timeout(function(){
$ionicLoading.hide();
},100,false);
}
function onError(error) {
console.log('*** error ContactsCtrl in $cordovaContactsFind: ' + angular.toJson(error));
$timeout(function(){
$ionicLoading.hide();
},100,false);
var strError = '';
switch(error.code) {
case 0:
strError = 'UNKNOWN ERROR, code: ' + error.code;
break;
case 1:
strError = 'INVALID ARGUMENT ERROR, code: ' + error.code;
break;
case 2:
strError = 'TIMEOUT ERROR, code: ' + error.code;
break;
case 3:
strError = 'PENDING OPERATION ERROR, code: ' + error.code;
break;
case 4:
strError = 'IO ERROR, code: ' + error.code;
break;
case 5:
strError = 'NOT SUPPORTED ERROR, code: ' + error.code;
break;
case 20:
strError = 'PERMISSION DENIED ERROR, code: ' + error.code;
break;
default:
strError = 'Unhandled error...'
}
$ionicPopup.alert({
title: CONSTANTS.APP_NAME,
template: strError
});
}
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,
navigator.contacts.fieldType.emails
];
var fields = [
navigator.contacts.fieldType.displayName
, navigator.contacts.fieldType.name
];
// setup a function that will be called depending on success / failure of making a test call to contacts api
// this function will be called after 30 seconds unless a test contacts call is successful
var contactTestFailureNotif = $timeout(function(){
$ionicLoading.hide();
$ionicPopup.alert({
title: CONSTANTS.APP_NAME,
template: CONSTANTS.ERROR_ACCESS_CONTACTS
});
}, 30 * 1000, true);
// setup a function that will be called depending on success / failure of loading contacts
// this function will be called after 30 seconds unless contacts loaded successfully
var contactLoadFailureNotif = $timeout(function(){
$ionicLoading.hide();
$ionicPopup.alert({
title: CONSTANTS.APP_NAME,
template: CONSTANTS.ERROR_LOAD_CONTACTS
});
}, 30 * 1000, true);
// make a test call to contacts plugin, if success - load all contacts and
// set contactsLoadSuccess to true
//watch if contacts loaded successfully
$scope.$watch('contactsMonitor', function(val) {
if(val.contactsTestSuccess === true){
//cancel timeout function call, contacts loaded successfully
//console.log('*** cancelling contact ** test ** failure notification');
$timeout.cancel(contactTestFailureNotif);
}
if(val.contactsLoadSuccess === true){
//cancel timeout function call, contacts loaded successfully
//console.log('*** cancelling contact ** load ** failure notification');
$timeout.cancel(contactLoadFailureNotif);
}
}, true);
//set filter to any string just to make a test request to contacts
options.filter = "zzz";
navigator.contacts.find(fields, function(data){
//console.log('*** test load of contacts successful : ContactsCtrl');
// cancel contacts failure notification
$scope.contactsMonitor.contactsTestSuccess = true;
// if test call succeeds, load all user's contacts
//load all contacts by not filtering results
options.filter = "";
navigator.contacts.find(fields, onSuccess, onError, options);
}, onError, options);