Realm Query Freezing App для большого количества записей - PullRequest
0 голосов
/ 16 апреля 2020

Я выполняю запрос области, чтобы получить список участников, после чего я передаю отдельного участника в качестве параметра методу getCommunity (участник), чтобы получить область, к которой принадлежит участник. Затем я впереди go и добавляю всех участников данного сообщества в Список, чтобы отобразить их в виде переработчика. Ниже приведен код:

List<StoredParticipant> FilteredMembers = new ArrayList<>();
        Realm realm = Realm.getDefaultInstance();
        try {
            realm.executeTransaction(realm1 -> {
                RealmResults<StoredParticipant> participants = realm1.where(StoredParticipant.class).beginGroup()
                        .isNull("isDeceased")
                        .or()
                        .notEqualTo("isDeceased", true)
                        .endGroup().findAll();

                for (StoredParticipant storedParticipant : participants) {
                    String community = getCommunity(storedParticipant);
                    if (community.equalsIgnoreCase(Community)) {
                        FilteredMembers.add(storedParticipant);

                    }
                }
            });

        } catch (Exception e) {

        }

Строка, вызывающая задержку:
Строка сообщества = getCommunity (сохраненный участник);

Как мне переписать код, чтобы предотвратить задержку?

Редактировать: код для getCommunity () выглядит следующим образом:

  public static String getCommunity(StoredParticipant participant) {
    String community = "";

    try (Realm realm = Realm.getDefaultInstance()) {
        HouseholdRelationship hhr = realm.where(HouseholdRelationship.class)
                .equalTo("participantId", participant.getId())
                .isNull("endDate")
                .findFirst();

        if (hhr != null) {
            Household household = realm.where(Household.class)
                    .equalTo("id", hhr.getHouseholdId())
                    .findFirst();

            if (household != null) {
                HouseholdAddress address = HouseholdService.getCurrentAddress(household);
                if (address != null) {
                    community = AreaService.getCommunityName(address.getArea());

                }
            }
        }
    }

    return community;
}
...