Realm Crashing on Moto G2(Lollipop),但适用于HTC Desire 620g(Kitkat)

时间:2015-11-03 06:59:54

标签: android realm

这是抛出我的函数 ArrayIndexOutOfBoundsException:rowIndex>可用行。在第if (!name.equalsIgnoreCase(realmResults.get(0).getName()) || !phoneNo.equalsIgnoreCase(realmResults.get(0).getPhone()) )

代码在HTC 620g上正常运行,但在Moto G2和其他设备上崩溃。

public static void refreshingContactsDB(final Context context)
{
    Thread background = new Thread(new Runnable()
    {
        public void run()
        {
            realmFresh = Realm.getInstance(context);

            createCountryDetailsArrayModel();

            TelephonyManager tm = (TelephonyManager) ApplicationController.getInstance()
                    .getSystemService(Context.TELEPHONY_SERVICE);
            String simCountryISO = tm.getSimCountryIso();

            for (int i = 0; i < countryDetailsList.size(); i++) {
                if (simCountryISO.equalsIgnoreCase(countryDetailsList.get(i)
                        .getCode())) {

                    dialCodePrefix = countryDetailsList.get(i).getDial_code();

                }
            }

            AppPreferenceManager.getInstance().setContactUpdate(false);

            Logger.debug("Contact Update Refreshing Contact Started -->");


            ContentResolver cr = ApplicationController.getInstance()
                    .getContentResolver();

            Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, null);

            String duplicateName = "";
            String duplicatePhone = "";

            if (phones.getCount() > 0) {

                final int nameIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
                final int numberIndex = phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

                while (phones.moveToNext()) {
                    String name = phones.getString(nameIndex);
                    String phoneNo = phones.getString(numberIndex);

                    if (phoneNo != null && !phoneNo.isEmpty())
                    {
                        phoneNo = phoneNo.replace(" ", "");
                        phoneNo = phoneNo.replace("(", "");
                        phoneNo = phoneNo.replace(")", "");
                        phoneNo = phoneNo.replace("-", "");
                        phoneNo = phoneNo.replace("\u00a0", "");
                        phoneNo = phoneNo.replace("\u202c", "");
                        phoneNo = phoneNo.replace("\u2011", "");
                        phoneNo = phoneNo.replace("\u202a", "");
                        phoneNo = phoneNo.replace("*", "");
                        phoneNo = phoneNo.replace("#", "");

                        if (phoneNo.length() >= 5) {

                            if (name.equalsIgnoreCase(duplicateName) && phoneNo.equalsIgnoreCase(duplicatePhone)) {
                                continue;
                            }

                            duplicateName = name;
                            duplicatePhone = phoneNo;

                            String formattedPhoneNumber;
                            formattedPhoneNumber = parseNumber(phoneNo);

                            RealmResults<R_LocalContactDB> realmResults = realmFresh.where(R_LocalContactDB.class).equalTo("phone", formattedPhoneNumber).findAll();
                            Logger.debug("Size: " + realmResults);
                            RealmResults<R_LocalContactDB> query = realmFresh.where(R_LocalContactDB.class).findAll();
                            R_LocalContactDB rContacts = new R_LocalContactDB(null, null, false, 0);


                            if (realmResults.size() == 0)
                            {
                                i++;
                                realmFresh.beginTransaction();
                                R_LocalContactDB rCont = realmFresh.copyToRealm(rContacts);
                                rCont.setName(name);
                                rCont.setPhone(formattedPhoneNumber);
                                rCont.setStatus(0);
                                rCont.setMatchedWithRecent(true);
                                Logger.debug("New Size: " + query.size());
                                realmFresh.commitTransaction();

                                Logger.debug("Sending To Server: " + i);
                                Logger.debug("Contact Update " + name
                                        + " saved");
                            }
                            else
                            {
                                realmFresh.beginTransaction();
                                if (!name.equalsIgnoreCase(realmResults.get(0).getName()) || !phoneNo.equalsIgnoreCase(realmResults.get(0).getPhone()) )
                                {
                                    realmResults.get(0).setName(name);
                                    realmResults.get(0).setPhone(phoneNo);
                                }


                                realmResults.get(0).setMatchedWithRecent(true);
                                realmResults.get(0);
                                Logger.debug("New Size Else Condition: " + query.size());
                                realmFresh.commitTransaction();


                            }
                        }
                    }
                }
                ContactsSyncService.contactUpdated = false;

                phones.close();
            }
            realmFresh.close();

            getNumbersFromDBAndUpdate();
            deleteExtraContacts();
        }
    });
    background.start();
}

1 个答案:

答案 0 :(得分:0)

RealmResults自动更新,因此当您编辑数据时,查询将被刷新。在你的情况下,它看起来像你在做:

RealmResults<R_LocalContactDB> realmResults = realmFresh.where(R_LocalContactDB.class).equalTo("phone", formattedPhoneNumber).findAll();


if (!name.equalsIgnoreCase(realmResults.get(0).getName()) || !phoneNo.equalsIgnoreCase(realmResults.get(0).getPhone()) ) {
  realmResults.get(0).setName(name);
  realmResults.get(0).setPhone(phoneNo); // This will make get(0) no longer part of the resultSet
}

realmResults.get(0).setMatchedWithRecent(true);

不要在任何地方使用.get(0)而只是缓存它:

R_LocalContactDB item = realmResults.get(0);
//...
item.setMatchedWithRecent(true);