Commit 50453051 authored by Tiago Cunha's avatar Tiago Cunha

Closing the realm. A little crude solution

parent 68e6306b
package chat.rocket.android.repositories;
import android.os.Handler;
import android.os.Looper;
import io.realm.Realm;
public class RealmRepository {
protected void close(Realm realm, Looper looper) {
new Handler(looper).post(new Runnable() {
@Override
public void run() {
realm.close();
}
});
}
}
package chat.rocket.android.repositories;
import android.os.Looper;
import io.realm.Realm;
import io.realm.RealmResults;
......@@ -11,8 +12,9 @@ import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.model.internal.LoadMessageProcedure;
import chat.rocket.persistence.realm.RealmStore;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
public class RealmRoomRepository implements RoomRepository {
public class RealmRoomRepository extends RealmRepository implements RoomRepository {
private final String hostname;
......@@ -24,6 +26,7 @@ public class RealmRoomRepository implements RoomRepository {
public Observable<List<Room>> getOpenRooms() {
return Observable.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
return Observable.just(null);
......@@ -33,6 +36,8 @@ public class RealmRoomRepository implements RoomRepository {
.equalTo(RoomSubscription.OPEN, true)
.findAll()
.asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(roomSubscriptions -> roomSubscriptions != null && roomSubscriptions.isLoaded()
&& roomSubscriptions.isValid())
.map(roomSubscriptions -> toList(roomSubscriptions));
......@@ -43,6 +48,7 @@ public class RealmRoomRepository implements RoomRepository {
public Observable<Room> getById(String roomId) {
return Observable.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
return Observable.just(null);
......@@ -52,6 +58,8 @@ public class RealmRoomRepository implements RoomRepository {
.equalTo(RoomSubscription.ROOM_ID, roomId)
.findFirstAsync()
.<RoomSubscription>asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(roomSubscription -> roomSubscription != null && roomSubscription.isLoaded()
&& roomSubscription.isValid())
.map(roomSubscription -> roomSubscription.asRoom());
......@@ -62,6 +70,7 @@ public class RealmRoomRepository implements RoomRepository {
public Observable<RoomHistoryState> getHistoryStateByRoomId(String roomId) {
return Observable.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
return Observable.just(null);
......@@ -71,6 +80,8 @@ public class RealmRoomRepository implements RoomRepository {
.equalTo(LoadMessageProcedure.ID, roomId)
.findFirstAsync()
.<LoadMessageProcedure>asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(loadMessageProcedure -> loadMessageProcedure != null
&& loadMessageProcedure.isLoaded() && loadMessageProcedure.isValid())
.map(loadMessageProcedure -> loadMessageProcedure.asRoomHistoryState());
......
package chat.rocket.android.repositories;
import android.os.Looper;
import io.realm.Realm;
import chat.rocket.android.model.core.User;
import chat.rocket.android.model.ddp.RealmUser;
import chat.rocket.persistence.realm.RealmStore;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
public class RealmUserRepository implements UserRepository {
public class RealmUserRepository extends RealmRepository implements UserRepository {
private final String hostname;
......@@ -19,6 +21,7 @@ public class RealmUserRepository implements UserRepository {
public Observable<User> getCurrentUser() {
return Observable.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) {
return Observable.just(null);
......@@ -29,11 +32,14 @@ public class RealmUserRepository implements UserRepository {
.findFirst();
if (realmUser == null) {
realm.close();
return Observable.just(null);
}
return realmUser
.<RealmUser>asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(it -> it != null && it.isLoaded() && it.isValid())
.map(it -> it.asUser());
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment