How to reload contacts data with Ionic and Cordova contacts?

Viewed 95

In an Ionic 4 app, I am using Cordova Contacts to retrieve all the device contacts, through the find method. It works fine, except that when I retrieve contacts in the app, then switch to the phone app, modify a contact (e.g. adding an email address), save it, revert to my app, and access the contact list again, it is not refreshed with the new email address, even though the find method is called again.

However if I quit the app and restart, then the contact changes appear.

The Contacts object is a field of a Ionic/Angular service, initialized with the service constructor. I have also tried to re-create this object whenever the find method is called, but it does not help.

export class DeviceContactService {
  constructor(private contacts: Contacts) { }

  getDeviceContacts(): Observable<any> {
    // this.contacts = new Contacts() // does not work better
    let fields : ContactFieldType[] = [ "displayName"]
    let options = new ContactFindOptions();
    options.filter = ""
    options.multiple = true
    options.desiredFields = [ 
      "displayName",  
      "name", 
      "emails", 
      "phoneNumbers" 
    ]
    let prom = this.contacts.find(fields, options)
    ...
  }
}

I expected the promise to return a list of contacts with the latest information stored in the device contact database, but it does not happen. It always return the data as they were before the contact database was changed.

1 Answers

You will probably also find that result varies whether the user is storing their contacts in iCloud (which uses a push from the server to refresh its data) or somewhere else (which uses fetches- the user can specify "automatic" or intervals down to 15 minutes from the Contacts page in Settings, but no faster)

Related