Obtaining contacts from content provider without d

2020-07-26 11:52发布

问题:

I have this code (thankfully provided by @EpicPandaForce) and I have problem with deletion. When I add new contact it works like a charm while when I delete contact (or number from that contact if there are two of them) it stays persisted in Realm. How can I get it working properly?

realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            Contact realmContact = new Contact();
            String filter = "" + ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and "
                    + ContactsContract.CommonDataKinds.Phone.TYPE +"="
                    + ContactsContract.CommonDataKinds.Phone.TYPE_MAIN;

            Cursor phones = getActivity()
                    .getContentResolver()
                    .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, filter, null, null);

            while (phones.moveToNext()) {
                String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
                String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                realmContact.setId(id);
                realmContact.setName(name);
                realmContact.setNumber(phoneNumber);
                realmContact.setIsBeingSaved(true);
                realm.insertOrUpdate(realmContact);
            }

            /** merge mechanism */
            realm.where(Contact.class)
                    .equalTo("isBeingSaved", false)
                    .findAll()
                    .deleteAllFromRealm(); // delete all non-saved data
            for(Contact contact : realm.where(Contact.class).findAll()) {
                realmContact.setIsBeingSaved(false); // reset all save state
            }

Contact.class

public class Contact extends RealmObject{

    @PrimaryKey
    private String id;

    @Index
    private String name;

    @Index
    private String number;

    @Index
    private boolean isBeingSaved;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public boolean getIsBeingSaved() {
        return isBeingSaved;
    }

    public void setIsBeingSaved(boolean beingSaved) {
        isBeingSaved = beingSaved;
    }
}

EDIT - working code:

Contact realmContact = new Contact();

            Uri uri = Contacts.CONTENT_URI;

            String selection = "((" + CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " NOTNULL) AND ("
                    + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
                    + CommonDataKinds.Phone.DISPLAY_NAME_PRIMARY + " != '' ))";

            Cursor phones = getActivity()
                    .getContentResolver()
                    .query(uri, null, selection, null, null);

            String phoneNumber = "";
            while (phones.moveToNext()) {
                String id = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
                String name = phones.getString(phones.getColumnIndex(Contacts.DISPLAY_NAME_PRIMARY));
                String lastTimeContacted = phones.getString(phones.getColumnIndex(Contacts.LAST_TIME_CONTACTED));

                if(Integer.parseInt(phones.getString(phones.getColumnIndex(Contacts.HAS_PHONE_NUMBER))) > 0){
                    Cursor pCur = getActivity().getContentResolver().query(
                            CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            CommonDataKinds.Phone.LOOKUP_KEY +" = ?",
                            new String[]{id}, null);

                    while (pCur.moveToNext()) {
                        phoneNumber += "/" + pCur.getString(pCur.getColumnIndex(CommonDataKinds.Phone.NUMBER));
                        realmContact.setNumber(phoneNumber);
                    }
                    phoneNumber = "";
                    pCur.close();
                } else {
                    realmContact.setNumber("1234");
                }
                realmContact.setId(id);
                realmContact.setName(lastTimeContacted);
                realmContact.setIsBeingSaved(true);
                realm.insertOrUpdate(realmContact);
            }

            Log.i("asd-size", realm.where(Contact.class).findAll().size() + "");

            /** merge mechanism */
            realm.where(Contact.class)
                    .equalTo("isBeingSaved", false)
                    .findAll()
                    .deleteAllFromRealm(); // delete all non-saved data


            for(Contact contact : realm.where(Contact.class).findAll()) {
                contact.setIsBeingSaved(false); // reset all save state
            }

回答1:

Okay, after quite long searching I found that there was a typo:

        /** merge mechanism */
        realm.where(Contact.class)
                .equalTo("isBeingSaved", false)
                .findAll()
                .deleteAllFromRealm(); // delete all non-saved data
        for(Contact contact : realm.where(Contact.class).findAll()) {
            realmContact.setIsBeingSaved(false); <- here it is
        }

It's realmContact when in fact it should be contact so it refers to contact iterated by for loop.

So, basically, it was setting Contact realmContact = new Contact(); to false. So the proper version is:

           /** merge mechanism */
            realm.where(Contact.class)
                    .equalTo("isBeingSaved", false)
                    .findAll()
                    .deleteAllFromRealm(); // delete all non-saved data
            for(Contact contact : realm.where(Contact.class).findAll()) {
                contact.setIsBeingSaved(false);
            }