Context.ContentResolver.Delete returns 0 for some contacts

sudhir.9350sudhir.9350 INUniversity ✭✭

Hi,

I have a weird issue. I am trying to delete all contacts from device. I had 150 contacts in device, so it deleted around 100 contacts but rest 50 are not deleting.

I use following code to delete the contact.

public bool DeleteRecord(string phoneNumber, string email)
{
    var Id = "0";
    var isDeleted = false;
    string lookupKey = string.Empty;
    try
    {

        var uri = ContactsContract.Contacts.ContentUri;
        string[] projection = {
            InterfaceConsts.Id,
            InterfaceConsts.DisplayName,
            ContactsContract.Contacts.InterfaceConsts.LookupKey
            };
        var uri1 = Android.Net.Uri.WithAppendedPath(ContactsContract.PhoneLookup.ContentFilterUri, Android.Net.Uri.Encode(phoneNumber));

        var cursor = appContext.ContentResolver.Query(uri1, projection, null, null, null);


        if (cursor.MoveToFirst())
        {
            do
            {
                var id = cursor.GetString(cursor.GetColumnIndex(projection[0]));
                var nm = cursor.GetString(cursor.GetColumnIndex(projection[1]));
                Id = id;

                if (Id != "0")
                {
                    int idx = cursor.GetColumnIndex(ContactsContract.ContactsColumns.LookupKey);
                    lookupKey = cursor.GetString(idx);
                }
                break;
            } while (cursor.MoveToNext());
            cursor.Close();
        }
        isDeleted = Id != "0";
        if (isDeleted)
        {
            var u = Android.Net.Uri.WithAppendedPath(ContactsContract.Contacts.ContentLookupUri, lookupKey);
            var res = appContext.ContentResolver.Delete(uri1, "contact_id=" + Id, null);
            return true;
        }
    }
    catch (Exception ex)
    {
        Android.Util.Log.Error("Error:", ex.Message + " " + ex.StackTrace);
    }


    return false;
}
Sign In or Register to comment.