android - Obtaining contacts from content provider without duplicates or invalid contacts, and save to Realm -
i have code (thankfully provided @epicpandaforce) , have problem deletion. when add new contact works charm while when delete contact (or number contact if there 2 of them) stays persisted in realm. how can 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 , " + 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 non-saved data for(contact contact : realm.where(contact.class).findall()) { realmcontact.setisbeingsaved(false); // reset 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) , (" + contacts.has_phone_number + "=1) , (" + 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 non-saved data for(contact contact : realm.where(contact.class).findall()) { contact.setisbeingsaved(false); // reset save state }
okay, after quite long searching found there typo:
/** merge mechanism */ realm.where(contact.class) .equalto("isbeingsaved", false) .findall() .deleteallfromrealm(); // delete non-saved data for(contact contact : realm.where(contact.class).findall()) { realmcontact.setisbeingsaved(false); <- here }
it's realmcontact
when in fact should contact
refers contact iterated for
loop.
so, basically, setting contact realmcontact = new contact();
false
. proper version is:
/** merge mechanism */ realm.where(contact.class) .equalto("isbeingsaved", false) .findall() .deleteallfromrealm(); // delete non-saved data for(contact contact : realm.where(contact.class).findall()) { contact.setisbeingsaved(false); }
Comments
Post a Comment