title: How apps like Truecaller, messenger maintains sync with native contacts application if any contact changes in the native contact app
body: <p>I have observed if we update/delete a the contact in native contacts application same changes is reflected into the TrueCaller instantly.</p>
  
  <p><strong>I can think of two ways it get the changes:</strong></p>
  
  <ol>
  <li>It refresh the whole contact list by deleting all the existing entries in app and query all the entries from native contact using contact provider every time app comes to foreground.But this approach might involve querying the RawContact table and Data table on every launch. (This might be a costly in terms of computation)</li>
  <li>Each raw contact maintains <a href="https://developer.android.com/reference/android/provider/ContactsContract.SyncColumns.html#VERSION" rel="nofollow noreferrer">VERSION</a> which increments if any change is made to the raw contact via native app. This can be leveraged for modifications but will require to maintain the old values in the truecaller(or similar app) so that we can compare.This approach will still require you query the RawContacts table.</li>
  </ol>
  
  <p>The later approach requires to maintain the <strong>Version</strong> on the TrueCaller app database as well.</p>
  
  <p><strong>Following operations will be required to detect any changes :</strong></p>
  
  <ol>
  <li>Check the version of the raw contact in the TrueCaller if the version is less than that of raw contact in native contacts db, then, query the corresponding Data table for all the changes.</li>
  <li>If any raw contact is deleted from native then it's either removed from the RawContact table or marked <strong>Deleted</strong>. Thus we need to check for <em>Deleted</em> flag or if any pervious RawContact entry is missing in the RawContact table.</li>
  <li>Other cases like a new contact is added hence a new raw contact entry in the RawContact table will be found etc.</li>
  </ol>
  
  <p>Is there any better way to import the contact in your app and maintain proper synchronization if any operation like addition, deletion or modification is performed in the native app?</p>
  
  <p>Certainly, app like TrueCaller does a great job with this. Any article on import contacts etc any relevant source will be appreciated.</p>