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; package chat.rocket.android.repositories;
import android.os.Looper;
import io.realm.Realm; import io.realm.Realm;
import io.realm.RealmResults; import io.realm.RealmResults;
...@@ -11,8 +12,9 @@ import chat.rocket.android.model.ddp.RoomSubscription; ...@@ -11,8 +12,9 @@ import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.model.internal.LoadMessageProcedure; import chat.rocket.android.model.internal.LoadMessageProcedure;
import chat.rocket.persistence.realm.RealmStore; import chat.rocket.persistence.realm.RealmStore;
import rx.Observable; import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
public class RealmRoomRepository implements RoomRepository { public class RealmRoomRepository extends RealmRepository implements RoomRepository {
private final String hostname; private final String hostname;
...@@ -24,6 +26,7 @@ public class RealmRoomRepository implements RoomRepository { ...@@ -24,6 +26,7 @@ public class RealmRoomRepository implements RoomRepository {
public Observable<List<Room>> getOpenRooms() { public Observable<List<Room>> getOpenRooms() {
return Observable.defer(() -> { return Observable.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname); final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) { if (realm == null) {
return Observable.just(null); return Observable.just(null);
...@@ -33,6 +36,8 @@ public class RealmRoomRepository implements RoomRepository { ...@@ -33,6 +36,8 @@ public class RealmRoomRepository implements RoomRepository {
.equalTo(RoomSubscription.OPEN, true) .equalTo(RoomSubscription.OPEN, true)
.findAll() .findAll()
.asObservable() .asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(roomSubscriptions -> roomSubscriptions != null && roomSubscriptions.isLoaded() .filter(roomSubscriptions -> roomSubscriptions != null && roomSubscriptions.isLoaded()
&& roomSubscriptions.isValid()) && roomSubscriptions.isValid())
.map(roomSubscriptions -> toList(roomSubscriptions)); .map(roomSubscriptions -> toList(roomSubscriptions));
...@@ -43,6 +48,7 @@ public class RealmRoomRepository implements RoomRepository { ...@@ -43,6 +48,7 @@ public class RealmRoomRepository implements RoomRepository {
public Observable<Room> getById(String roomId) { public Observable<Room> getById(String roomId) {
return Observable.defer(() -> { return Observable.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname); final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) { if (realm == null) {
return Observable.just(null); return Observable.just(null);
...@@ -52,6 +58,8 @@ public class RealmRoomRepository implements RoomRepository { ...@@ -52,6 +58,8 @@ public class RealmRoomRepository implements RoomRepository {
.equalTo(RoomSubscription.ROOM_ID, roomId) .equalTo(RoomSubscription.ROOM_ID, roomId)
.findFirstAsync() .findFirstAsync()
.<RoomSubscription>asObservable() .<RoomSubscription>asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(roomSubscription -> roomSubscription != null && roomSubscription.isLoaded() .filter(roomSubscription -> roomSubscription != null && roomSubscription.isLoaded()
&& roomSubscription.isValid()) && roomSubscription.isValid())
.map(roomSubscription -> roomSubscription.asRoom()); .map(roomSubscription -> roomSubscription.asRoom());
...@@ -62,6 +70,7 @@ public class RealmRoomRepository implements RoomRepository { ...@@ -62,6 +70,7 @@ public class RealmRoomRepository implements RoomRepository {
public Observable<RoomHistoryState> getHistoryStateByRoomId(String roomId) { public Observable<RoomHistoryState> getHistoryStateByRoomId(String roomId) {
return Observable.defer(() -> { return Observable.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname); final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) { if (realm == null) {
return Observable.just(null); return Observable.just(null);
...@@ -71,6 +80,8 @@ public class RealmRoomRepository implements RoomRepository { ...@@ -71,6 +80,8 @@ public class RealmRoomRepository implements RoomRepository {
.equalTo(LoadMessageProcedure.ID, roomId) .equalTo(LoadMessageProcedure.ID, roomId)
.findFirstAsync() .findFirstAsync()
.<LoadMessageProcedure>asObservable() .<LoadMessageProcedure>asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(loadMessageProcedure -> loadMessageProcedure != null .filter(loadMessageProcedure -> loadMessageProcedure != null
&& loadMessageProcedure.isLoaded() && loadMessageProcedure.isValid()) && loadMessageProcedure.isLoaded() && loadMessageProcedure.isValid())
.map(loadMessageProcedure -> loadMessageProcedure.asRoomHistoryState()); .map(loadMessageProcedure -> loadMessageProcedure.asRoomHistoryState());
......
package chat.rocket.android.repositories; package chat.rocket.android.repositories;
import android.os.Looper;
import io.realm.Realm; import io.realm.Realm;
import chat.rocket.android.model.core.User; import chat.rocket.android.model.core.User;
import chat.rocket.android.model.ddp.RealmUser; import chat.rocket.android.model.ddp.RealmUser;
import chat.rocket.persistence.realm.RealmStore; import chat.rocket.persistence.realm.RealmStore;
import rx.Observable; import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
public class RealmUserRepository implements UserRepository { public class RealmUserRepository extends RealmRepository implements UserRepository {
private final String hostname; private final String hostname;
...@@ -19,6 +21,7 @@ public class RealmUserRepository implements UserRepository { ...@@ -19,6 +21,7 @@ public class RealmUserRepository implements UserRepository {
public Observable<User> getCurrentUser() { public Observable<User> getCurrentUser() {
return Observable.defer(() -> { return Observable.defer(() -> {
final Realm realm = RealmStore.getRealm(hostname); final Realm realm = RealmStore.getRealm(hostname);
final Looper looper = Looper.myLooper();
if (realm == null) { if (realm == null) {
return Observable.just(null); return Observable.just(null);
...@@ -29,11 +32,14 @@ public class RealmUserRepository implements UserRepository { ...@@ -29,11 +32,14 @@ public class RealmUserRepository implements UserRepository {
.findFirst(); .findFirst();
if (realmUser == null) { if (realmUser == null) {
realm.close();
return Observable.just(null); return Observable.just(null);
} }
return realmUser return realmUser
.<RealmUser>asObservable() .<RealmUser>asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper))
.filter(it -> it != null && it.isLoaded() && it.isValid()) .filter(it -> it != null && it.isLoaded() && it.isValid())
.map(it -> it.asUser()); .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