Skip to content

Commit

Permalink
Fix realm transactions with close
Browse files Browse the repository at this point in the history
  • Loading branch information
tspoke committed Mar 8, 2017
1 parent 287be6a commit 1b5d350
Showing 1 changed file with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ public final Observable<T> getOne(Filter filter, SortingMode sortingMode) {
query = filterToQuery(filter, query);
T item = query.findFirst();

T copy = null;
if(item != null) {
return Observable.just(realm.copyFromRealm(item));
} else {
return Observable.just(null);
realm.copyFromRealm(item);
}
realm.close();
return Observable.just(copy);
}

/**
Expand All @@ -68,8 +69,10 @@ public final Observable<List<T>> getAll(Filter filter, SortingMode sortingMode)
RealmQuery<T> query = realm.where(clazz);
query = filterToQuery(filter, query);
RealmResults<T> items = query.findAllSorted(sortingMode.key, convertToSort(sortingMode.sort));
List<T> copies = realm.copyFromRealm(items);
realm.close();

return Observable.just(realm.copyFromRealm(items));
return Observable.just(copies);
}

/**
Expand All @@ -82,8 +85,10 @@ public final Observable<T> insertOrUpdate(T object) {
realm.beginTransaction();
T inserted = realm.copyToRealmOrUpdate(object);
realm.commitTransaction();
T copy = realm.copyFromRealm(inserted);
realm.close();

return Observable.just(realm.copyFromRealm(inserted));
return Observable.just(copy);
}

/**
Expand All @@ -97,8 +102,10 @@ public final Observable<List<T>> insertOrUpdate(List<T> items) {
realm.beginTransaction();
items = realm.copyToRealmOrUpdate(items);
realm.commitTransaction();
List<T> copies = realm.copyFromRealm(items);
realm.close();

return Observable.just(realm.copyFromRealm(items));
return Observable.just(copies);
}

/**
Expand Down Expand Up @@ -130,6 +137,8 @@ public Observable<Void> deleteAll() {
realm.beginTransaction();
realm.delete(clazz);
realm.commitTransaction();
realm.close();

return Observable.just(null);
}

Expand Down

0 comments on commit 1b5d350

Please sign in to comment.