use of io.realm.RealmConfiguration in project realm-java by realm.
the class RealmObservableFactory method changesetsFrom.
@Override
public <E extends RealmModel> Observable<ObjectChange<E>> changesetsFrom(Realm realm, final E object) {
if (realm.isFrozen()) {
return Observable.just(new ObjectChange<E>(object, null));
}
final RealmConfiguration realmConfig = realm.getConfiguration();
Scheduler scheduler = getScheduler();
return Observable.create(new ObservableOnSubscribe<ObjectChange<E>>() {
@Override
public void subscribe(final ObservableEmitter<ObjectChange<E>> emitter) {
// If the Realm has been closed, just create an empty Observable because we assume it is going to be disposed shortly.
if (!RealmObject.isValid(object))
return;
// Gets instance to make sure that the Realm is open for as long as the
// Observable is subscribed to it.
final Realm observableRealm = Realm.getInstance(realmConfig);
objectRefs.get().acquireReference(object);
final RealmObjectChangeListener<E> listener = new RealmObjectChangeListener<E>() {
@Override
public void onChange(E obj, ObjectChangeSet changeSet) {
if (!emitter.isDisposed()) {
emitter.onNext(new ObjectChange<>(returnFrozenObjects ? RealmObject.freeze(obj) : obj, changeSet));
}
}
};
RealmObject.addChangeListener(object, listener);
// Cleanup when stream is disposed
emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
if (!observableRealm.isClosed()) {
RealmObject.removeChangeListener(object, listener);
observableRealm.close();
}
objectRefs.get().releaseReference(object);
}
}));
// Emit current value immediately
emitter.onNext(new ObjectChange<>(returnFrozenObjects ? RealmObject.freeze(object) : object, null));
}
}).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
use of io.realm.RealmConfiguration in project realm-java by realm.
the class RealmObservableFactory method from.
@Override
public Flowable<DynamicRealmObject> from(DynamicRealm realm, final DynamicRealmObject object) {
if (realm.isFrozen()) {
return Flowable.just(object);
}
final RealmConfiguration realmConfig = realm.getConfiguration();
Scheduler scheduler = getScheduler();
return Flowable.create(new FlowableOnSubscribe<DynamicRealmObject>() {
@Override
public void subscribe(final FlowableEmitter<DynamicRealmObject> emitter) {
// If the Realm has been closed, just create an empty Observable because we assume it is going to be disposed shortly.
if (realm.isClosed())
return;
// Gets instance to make sure that the Realm is open for as long as the
// Observable is subscribed to it.
final DynamicRealm observableRealm = DynamicRealm.getInstance(realmConfig);
objectRefs.get().acquireReference(object);
final RealmChangeListener<DynamicRealmObject> listener = new RealmChangeListener<DynamicRealmObject>() {
@Override
public void onChange(DynamicRealmObject obj) {
if (!emitter.isCancelled()) {
emitter.onNext(returnFrozenObjects ? RealmObject.freeze(obj) : obj);
}
}
};
RealmObject.addChangeListener(object, listener);
// Cleanup when stream is disposed
emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
if (!observableRealm.isClosed()) {
RealmObject.removeChangeListener(object, listener);
observableRealm.close();
}
objectRefs.get().releaseReference(object);
}
}));
// Emit current value immediately
emitter.onNext(returnFrozenObjects ? RealmObject.freeze(object) : object);
}
}, BACK_PRESSURE_STRATEGY).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
use of io.realm.RealmConfiguration in project realm-java by realm.
the class RealmObservableFactory method from.
@Override
public <E> Flowable<RealmList<E>> from(DynamicRealm realm, final RealmList<E> list) {
if (realm.isFrozen()) {
return Flowable.just(list);
}
final RealmConfiguration realmConfig = realm.getConfiguration();
Scheduler scheduler = getScheduler();
return Flowable.create(new FlowableOnSubscribe<RealmList<E>>() {
@Override
public void subscribe(final FlowableEmitter<RealmList<E>> emitter) {
// If the Realm has been closed, just create an empty Observable because we assume it is going to be disposed shortly.
if (!list.isValid())
return;
// Gets instance to make sure that the Realm is open for as long as the
// Observable is subscribed to it.
final DynamicRealm observableRealm = DynamicRealm.getInstance(realmConfig);
listRefs.get().acquireReference(list);
final RealmChangeListener<RealmList<E>> listener = new RealmChangeListener<RealmList<E>>() {
@Override
public void onChange(RealmList<E> list) {
if (!list.isValid()) {
// Terminate stream if list is no longer valid. This indicates
// the parent object has been deleted, so no further updates
// will be coming.
emitter.onComplete();
return;
}
if (!emitter.isCancelled()) {
emitter.onNext(returnFrozenObjects ? list.freeze() : list);
}
}
};
list.addChangeListener(listener);
// Cleanup when stream is disposed
emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
if (!observableRealm.isClosed()) {
list.removeChangeListener(listener);
observableRealm.close();
}
listRefs.get().releaseReference(list);
}
}));
// Emit current value immediately
emitter.onNext(returnFrozenObjects ? list.freeze() : list);
}
}, BACK_PRESSURE_STRATEGY).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
use of io.realm.RealmConfiguration in project realm-java by realm.
the class SyncObjectServerFacade method getSyncConfigurationOptions.
@Override
public Object[] getSyncConfigurationOptions(RealmConfiguration config) {
if (config instanceof SyncConfiguration) {
SyncConfiguration syncConfig = (SyncConfiguration) config;
User user = syncConfig.getUser();
App app = user.getApp();
String rosServerUrl = syncConfig.getServerUrl().toString();
String rosUserIdentity = user.getId();
String syncRealmAuthUrl = user.getApp().getConfiguration().getBaseUrl().toString();
String rosUserProvider = user.getProviderType().getId();
String syncUserRefreshToken = user.getRefreshToken();
String syncUserAccessToken = user.getAccessToken();
String deviceId = user.getDeviceId();
byte sessionStopPolicy = syncConfig.getSessionStopPolicy().getNativeValue();
String urlPrefix = syncConfig.getUrlPrefix();
String customAuthorizationHeaderName = app.getConfiguration().getAuthorizationHeaderName();
Map<String, String> customHeaders = app.getConfiguration().getCustomRequestHeaders();
SyncClientResetStrategy clientResetStrategy = syncConfig.getSyncClientResetStrategy();
// undefined value
byte clientResetMode = -1;
if (clientResetStrategy instanceof ManuallyRecoverUnsyncedChangesStrategy) {
clientResetMode = OsRealmConfig.CLIENT_RESYNC_MODE_MANUAL;
} else if (clientResetStrategy instanceof DiscardUnsyncedChangesStrategy) {
clientResetMode = OsRealmConfig.CLIENT_RESYNC_MODE_DISCARD_LOCAL;
}
BeforeClientResetHandler beforeClientResetHandler = (localPtr, osRealmConfig) -> {
NativeContext.execute(nativeContext -> {
Realm before = realmInstanceFactory.createInstance(new OsSharedRealm(localPtr, osRealmConfig, nativeContext));
((DiscardUnsyncedChangesStrategy) clientResetStrategy).onBeforeReset(before);
});
};
AfterClientResetHandler afterClientResetHandler = (localPtr, afterPtr, osRealmConfig) -> {
NativeContext.execute(nativeContext -> {
Realm before = realmInstanceFactory.createInstance(new OsSharedRealm(localPtr, osRealmConfig, nativeContext));
Realm after = realmInstanceFactory.createInstance(new OsSharedRealm(afterPtr, osRealmConfig, nativeContext));
((DiscardUnsyncedChangesStrategy) clientResetStrategy).onAfterReset(before, after);
});
};
long appNativePointer;
// access it.
try {
if (osAppField == null) {
synchronized (SyncObjectServerFacade.class) {
if (osAppField == null) {
Field field = App.class.getDeclaredField("osApp");
field.setAccessible(true);
osAppField = field;
}
}
}
OsApp osApp = (OsApp) osAppField.get(app);
appNativePointer = osApp.getNativePtr();
} catch (Exception e) {
throw new RuntimeException(e);
}
// TODO Simplify. org.bson serialization only allows writing full documents, so the partition
// key is embedded in a document with key 'value' and unwrapped in JNI.
String encodedPartitionValue = null;
if (syncConfig.isPartitionBasedSyncConfiguration()) {
BsonValue partitionValue = syncConfig.getPartitionValue();
switch(partitionValue.getBsonType()) {
case STRING:
case OBJECT_ID:
case INT32:
case INT64:
case BINARY:
case NULL:
encodedPartitionValue = JniBsonProtocol.encode(partitionValue, AppConfiguration.DEFAULT_BSON_CODEC_REGISTRY);
break;
default:
throw new IllegalArgumentException("Unsupported type: " + partitionValue);
}
}
int i = 0;
Object[] configObj = new Object[SYNC_CONFIG_OPTIONS];
configObj[i++] = rosUserIdentity;
configObj[i++] = rosUserProvider;
configObj[i++] = rosServerUrl;
configObj[i++] = syncRealmAuthUrl;
configObj[i++] = syncUserRefreshToken;
configObj[i++] = syncUserAccessToken;
configObj[i++] = deviceId;
configObj[i++] = sessionStopPolicy;
configObj[i++] = urlPrefix;
configObj[i++] = customAuthorizationHeaderName;
configObj[i++] = customHeaders;
configObj[i++] = clientResetMode;
configObj[i++] = beforeClientResetHandler;
configObj[i++] = afterClientResetHandler;
configObj[i++] = encodedPartitionValue;
configObj[i++] = app.getSync();
configObj[i++] = appNativePointer;
return configObj;
} else {
return new Object[SYNC_CONFIG_OPTIONS];
}
}
use of io.realm.RealmConfiguration in project realm-java by realm.
the class RealmObservableFactory method from.
@Override
public <E extends RealmModel> Flowable<E> from(final Realm realm, final E object) {
if (realm.isFrozen()) {
return Flowable.just(object);
}
final RealmConfiguration realmConfig = realm.getConfiguration();
Scheduler scheduler = getScheduler();
return Flowable.create(new FlowableOnSubscribe<E>() {
@Override
public void subscribe(final FlowableEmitter<E> emitter) {
// If the Realm has been closed, just create an empty Observable because we assume it is going to be disposed shortly.
if (realm.isClosed())
return;
// Gets instance to make sure that the Realm is open for as long as the
// Observable is subscribed to it.
final Realm observableRealm = Realm.getInstance(realmConfig);
objectRefs.get().acquireReference(object);
final RealmChangeListener<E> listener = new RealmChangeListener<E>() {
@Override
public void onChange(E obj) {
if (!emitter.isCancelled()) {
emitter.onNext(returnFrozenObjects ? RealmObject.freeze(obj) : obj);
}
}
};
RealmObject.addChangeListener(object, listener);
// Cleanup when stream is disposed
emitter.setDisposable(Disposables.fromRunnable(new Runnable() {
@Override
public void run() {
if (!observableRealm.isClosed()) {
RealmObject.removeChangeListener(object, listener);
observableRealm.close();
}
objectRefs.get().releaseReference(object);
}
}));
// Emit current value immediately
emitter.onNext(returnFrozenObjects ? RealmObject.freeze(object) : object);
}
}, BACK_PRESSURE_STRATEGY).subscribeOn(scheduler).unsubscribeOn(scheduler);
}
Aggregations