Skip to content

Commit

Permalink
Adds Close Method
Browse files Browse the repository at this point in the history
Adds close method to ensure that realmResults are reset and that the change listener is removed.

Verified that doing the right thing and cleaning things up, fixed the fatal exception
on item deletion because the 'old' change listener is no longer called.
Resolves thorbenprimke#13
  • Loading branch information
thorbenprimke committed Jan 16, 2016
1 parent e987f41 commit 33a95f8
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions library/src/main/java/io/realm/RealmBasedRecyclerViewAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ public int getItemRealmViewType(int position) {
return super.getItemViewType(position);
}

/**
* Ensure {@link #close()} is called whenever {@link Realm#close()} is called to ensure that the
* {@link #realmResults} are invalidated and the change listener removed.
*/
public void close() {
updateRealmResults(null);
}

/**
* Update the RealmResults associated with the Adapter. Useful when the query has been changed.
* If the query does not change you might consider using the automaticUpdate feature.
Expand All @@ -313,15 +321,15 @@ public int getItemRealmViewType(int position) {
*/
public void updateRealmResults(RealmResults<T> queryResults) {
if (listener != null) {
if (this.realmResults != null) {
this.realmResults.realm.removeChangeListener(listener);
}
if (queryResults != null) {
queryResults.realm.addChangeListener(listener);
if (realmResults != null) {
realmResults.realm.removeChangeListener(listener);
}
}

this.realmResults = queryResults;
realmResults = queryResults;
if (realmResults != null) {
realmResults.realm.addChangeListener(listener);
}

updateRowWrappers();
ids = getIdsOfRealmResults();
Expand All @@ -338,7 +346,7 @@ public String createHeaderFromColumnValue(String columnValue) {
}

private List getIdsOfRealmResults() {
if (!animateResults || realmResults.size() == 0) {
if (!animateResults || realmResults == null || realmResults.size() == 0) {
return EMPTY_LIST;
}

Expand Down Expand Up @@ -390,6 +398,9 @@ private Object getRealmRowId(int realmIndex) {
}

private void updateRowWrappers() {
if (realmResults == null) {
return;
}
if (addSectionHeaders) {
String lastHeader = "";
int headerCount = 0;
Expand Down

0 comments on commit 33a95f8

Please sign in to comment.