use of io.realm.internal.RealmObjectProxy in project realm-java by realm.
the class RealmList method add.
/**
* Inserts the specified object into this List at the specified location. The object is inserted before any previous
* element at the specified location. If the location is equal to the size of this List, the object is added at the
* end.
* <ol>
* <li><b>Unmanaged RealmLists</b>: It is possible to add both managed and unmanaged objects. If adding managed
* objects to an unmanaged RealmList they will not be copied to the Realm again if using
* {@link Realm#copyToRealm(RealmModel)} afterwards.</li>
*
* <li><b>Managed RealmLists</b>: It is possible to add unmanaged objects to a RealmList that is already managed. In
* that case the object will transparently be copied to Realm using {@link Realm#copyToRealm(RealmModel)}
* or {@link Realm#copyToRealmOrUpdate(RealmModel)} if it has a primary key.</li>
* </ol>
*
* @param location the index at which to insert.
* @param object the object to add.
* @throws IllegalStateException if Realm instance has been closed or container object has been removed.
* @throws IndexOutOfBoundsException if {@code location < 0 || location > size()}.
*/
@Override
public void add(int location, E object) {
checkValidObject(object);
if (isManaged()) {
checkValidView();
if (location < 0 || location > size()) {
throw new IndexOutOfBoundsException("Invalid index " + location + ", size is " + size());
}
RealmObjectProxy proxy = (RealmObjectProxy) copyToRealmIfNeeded(object);
view.insert(location, proxy.realmGet$proxyState().getRow$realm().getIndex());
} else {
unmanagedList.add(location, object);
}
modCount++;
}
use of io.realm.internal.RealmObjectProxy in project realm-java by realm.
the class RealmObject method deleteFromRealm.
/**
* Deletes the object from the Realm it is currently associated with.
* <p>
* After this method is called the object will be invalid and any operation (read or write) performed on it will
* fail with an IllegalStateException.
*
* @throws IllegalStateException if the corresponding Realm is closed or in an incorrect thread.
* @see #isValid()
*/
public static <E extends RealmModel> void deleteFromRealm(E object) {
if (!(object instanceof RealmObjectProxy)) {
// TODO What type of exception IllegalArgument/IllegalState?
throw new IllegalArgumentException("Object not managed by Realm, so it cannot be removed.");
}
RealmObjectProxy proxy = (RealmObjectProxy) object;
if (proxy.realmGet$proxyState().getRow$realm() == null) {
throw new IllegalStateException("Object malformed: missing object in Realm. Make sure to instantiate RealmObjects with Realm.createObject()");
}
if (proxy.realmGet$proxyState().getRealm$realm() == null) {
throw new IllegalStateException("Object malformed: missing Realm. Make sure to instantiate RealmObjects with Realm.createObject()");
}
proxy.realmGet$proxyState().getRealm$realm().checkIfValid();
Row row = proxy.realmGet$proxyState().getRow$realm();
row.getTable().moveLastOver(row.getIndex());
proxy.realmGet$proxyState().setRow$realm(InvalidRow.INSTANCE);
}
use of io.realm.internal.RealmObjectProxy in project realm-java by realm.
the class RealmObject method removeChangeListener.
/**
* Removes a previously registered listener on the given RealmObject.
*
* @param object RealmObject to remove listener from.
* @param listener the instance to be removed.
* @throws IllegalArgumentException if the {@code object} or the change listener is {@code null}.
* @throws IllegalArgumentException if object is an unmanaged RealmObject.
* @throws IllegalStateException if you try to remove a listener from a non-Looper Thread.
*/
public static <E extends RealmModel> void removeChangeListener(E object, RealmChangeListener listener) {
if (object == null) {
throw new IllegalArgumentException("Object should not be null");
}
if (listener == null) {
throw new IllegalArgumentException("Listener should not be null");
}
if (object instanceof RealmObjectProxy) {
RealmObjectProxy proxy = (RealmObjectProxy) object;
BaseRealm realm = proxy.realmGet$proxyState().getRealm$realm();
realm.checkIfValid();
realm.sharedRealm.capabilities.checkCanDeliverNotification(BaseRealm.LISTENER_NOT_ALLOWED_MESSAGE);
//noinspection unchecked
proxy.realmGet$proxyState().removeChangeListener(listener);
} else {
throw new IllegalArgumentException("Cannot remove listener from this unmanaged RealmObject (created outside of Realm)");
}
}
use of io.realm.internal.RealmObjectProxy in project realm-java by realm.
the class RealmObject method addChangeListener.
/**
* Adds a change listener to a RealmObject.
*
* @param object RealmObject to add listener to.
* @param listener the change listener to be notified.
* @throws IllegalArgumentException if the {@code object} or the change listener is {@code null}.
* @throws IllegalArgumentException if object is an unmanaged RealmObject.
* @throws IllegalStateException if you try to add a listener from a non-Looper or {@link IntentService} thread.
*/
public static <E extends RealmModel> void addChangeListener(E object, RealmChangeListener<E> listener) {
if (object == null) {
throw new IllegalArgumentException("Object should not be null");
}
if (listener == null) {
throw new IllegalArgumentException("Listener should not be null");
}
if (object instanceof RealmObjectProxy) {
RealmObjectProxy proxy = (RealmObjectProxy) object;
BaseRealm realm = proxy.realmGet$proxyState().getRealm$realm();
realm.checkIfValid();
realm.sharedRealm.capabilities.checkCanDeliverNotification(BaseRealm.LISTENER_NOT_ALLOWED_MESSAGE);
//noinspection unchecked
proxy.realmGet$proxyState().addChangeListener(listener);
} else {
throw new IllegalArgumentException("Cannot add listener from this unmanaged RealmObject (created outside of Realm)");
}
}
use of io.realm.internal.RealmObjectProxy in project realm-java by realm.
the class RealmObject method asObservable.
/**
* Returns an RxJava Observable that monitors changes to this RealmObject. It will emit the current object when
* subscribed to. Object updates will continuously be emitted as the RealmObject is updated -
* {@code onComplete} will never be called.
* <p>
* When chaining a RealmObject observable use {@code obj.<MyRealmObjectClass>asObservable()} to pass on
* type information, otherwise the type of the following observables will be {@code RealmObject}.
* <p>
* If you would like the {@code asObservable()} to stop emitting items you can instruct RxJava to
* emit only the first item by using the {@code first()} operator:
*
* <pre>
* {@code
* obj.asObservable()
* .filter(obj -> obj.isLoaded())
* .first()
* .subscribe( ... ) // You only get the object once
* }
* </pre>
*
* @param object RealmObject class that is being observed. Must be this class or its super types.
* @return RxJava Observable that only calls {@code onNext}. It will never call {@code onComplete} or {@code OnError}.
* @throws UnsupportedOperationException if the required RxJava framework is not on the classpath.
* @see <a href="https://realm.io/docs/java/latest/#rxjava">RxJava and Realm</a>
*/
public static <E extends RealmModel> Observable<E> asObservable(E object) {
if (object instanceof RealmObjectProxy) {
RealmObjectProxy proxy = (RealmObjectProxy) object;
BaseRealm realm = proxy.realmGet$proxyState().getRealm$realm();
if (realm instanceof Realm) {
return realm.configuration.getRxFactory().from((Realm) realm, object);
} else if (realm instanceof DynamicRealm) {
DynamicRealm dynamicRealm = (DynamicRealm) realm;
DynamicRealmObject dynamicObject = (DynamicRealmObject) object;
@SuppressWarnings("unchecked") Observable<E> observable = (Observable<E>) realm.configuration.getRxFactory().from(dynamicRealm, dynamicObject);
return observable;
} else {
throw new UnsupportedOperationException(realm.getClass() + " does not support RxJava." + " See https://realm.io/docs/java/latest/#rxjava for more details.");
}
} else {
// TODO Is this true? Should we just return Observable.just(object) ?
throw new IllegalArgumentException("Cannot create Observables from unmanaged RealmObjects");
}
}
Aggregations