Cordova联系人插件有时不会加载联系人

时间:2015-04-16 19:38:15

标签: cordova ionic-framework cordova-plugins

我们有一个离子应用程序使用包装在ngcordova中的cordova联系人插件。我们加载在应用程序中使用的整个地址簿,以便在应用程序中使用。

有些用户报告只有100个联系人和联系人永远不会加载。

我遇到了这个问题,除了在Galaxy s1,4.2.2上加载700个联系人需要大约24秒。但是,我的一些用户无法加载任何只挂起的联系人​​,也从不隐藏加载指示符。

我想知道是否有人看到一个奇怪的接触角色或缺少的字段会导致加载触点的过程制动?此外,我有错误回调警报错误代码的警报,但它永远不会被调用...

非常感谢!

这是我的代码:

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);

0 个答案:

没有答案