Slide 21
Slide 21 text
public abstract class OnSubscribeRealm
implements Observable.OnSubscribe {
!
/** constructor **/
!
@Override
public void call(final Subscriber super T> subscriber) {
Realm realm = Realm.getInstance(context);
subscriber.add(Subscriptions.create(() -> {
try {
realm.close();
} catch (RealmException ex) {
subscriber.onError(ex);
}
}));
!
T object;
realm.beginTransaction();
try {
object = get(realm);
realm.commitTransaction();
} catch (RuntimeException e) {
realm.cancelTransaction();
subscriber.onError(new RealmException(Error during transaction., e));
return;
} catch (Error e) {
realm.cancelTransaction();
subscriber.onError(e);
return;
}
if (object != null) {
subscriber.onNext(object);
}
subscriber.onCompleted();
}
!
public abstract T get(Realm realm);
}