Search in sources :

Example 31 with RealmObjectProxy

use of io.realm.internal.RealmObjectProxy in project realm-java by realm.

the class RealmList method add.

/**
     * Adds the specified object at the end of this List.
     * <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 object the object to add.
     * @return always {@code true}.
     * @throws IllegalStateException if Realm instance has been closed or parent object has been removed.
     */
@Override
public boolean add(E object) {
    checkValidObject(object);
    if (isManaged()) {
        checkValidView();
        RealmObjectProxy proxy = (RealmObjectProxy) copyToRealmIfNeeded(object);
        view.add(proxy.realmGet$proxyState().getRow$realm().getIndex());
    } else {
        unmanagedList.add(object);
    }
    modCount++;
    return true;
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 32 with RealmObjectProxy

use of io.realm.internal.RealmObjectProxy in project realm-java by realm.

the class RealmList method set.

/**
     * Replaces the element at the specified location in this list with the specified object.
     * <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 put the specified object.
     * @param object the object to add.
     * @return the previous element at the index.
     * @throws IllegalStateException if Realm instance has been closed or parent object has been removed.
     * @throws IndexOutOfBoundsException if {@code location < 0 || location >= size()}.
     */
@Override
public E set(int location, E object) {
    checkValidObject(object);
    E oldObject;
    if (isManaged()) {
        checkValidView();
        RealmObjectProxy proxy = (RealmObjectProxy) copyToRealmIfNeeded(object);
        oldObject = get(location);
        view.set(location, proxy.realmGet$proxyState().getRow$realm().getIndex());
        return oldObject;
    } else {
        oldObject = unmanagedList.set(location, object);
    }
    return oldObject;
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 33 with RealmObjectProxy

use of io.realm.internal.RealmObjectProxy in project realm-java by realm.

the class RealmObject method removeAllChangeListeners.

/**
     * Removes all registered listeners from the given RealmObject.
     *
     * @param object RealmObject to remove all listeners from.
     * @throws IllegalArgumentException if object is {@code null} or isn't managed by Realm.
     */
public static <E extends RealmModel> void removeAllChangeListeners(E object) {
    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);
        proxy.realmGet$proxyState().removeAllChangeListeners();
    } else {
        throw new IllegalArgumentException("Cannot remove listeners from this unmanaged RealmObject (created outside of Realm)");
    }
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Example 34 with RealmObjectProxy

use of io.realm.internal.RealmObjectProxy in project realm-java by realm.

the class RealmObject method isValid.

/**
     * Checks if the RealmObject is still valid to use i.e., the RealmObject hasn't been deleted nor has the
     * {@link io.realm.Realm} been closed. It will always return {@code true} for unmanaged objects.
     *
     * @param object RealmObject to check validity for.
     * @return {@code true} if the object is still accessible or an unmanaged object, {@code false} otherwise.
     */
public static <E extends RealmModel> boolean isValid(E object) {
    if (object instanceof RealmObjectProxy) {
        RealmObjectProxy proxy = (RealmObjectProxy) object;
        Row row = proxy.realmGet$proxyState().getRow$realm();
        return row != null && row.isAttached();
    } else {
        return true;
    }
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy) Row(io.realm.internal.Row) InvalidRow(io.realm.internal.InvalidRow)

Example 35 with RealmObjectProxy

use of io.realm.internal.RealmObjectProxy in project realm-java by realm.

the class RealmObject method isLoaded.

/**
     * Checks if the query used to find this RealmObject has completed.
     *
     * Async methods like {@link RealmQuery#findFirstAsync()} return an {@link RealmObject} that represents the future result
     * of the {@link RealmQuery}. It can be considered similar to a {@link java.util.concurrent.Future} in this regard.
     *
     * Once {@code isLoaded()} returns {@code true}, the object represents the query result even if the query
     * didn't find any object matching the query parameters. In this case the {@link RealmObject} will
     * become a "null" object.
     *
     * "Null" objects represents {@code null}.  An exception is throw if any accessor is called, so it is important to also
     * check {@link #isValid()} before calling any methods. A common pattern is:
     *
     * <pre>
     * {@code
     * Person person = realm.where(Person.class).findFirstAsync();
     * RealmObject.isLoaded(person); // == false
     * RealmObject.addChangeListener(person, new RealmChangeListener() {
     *      \@Override
     *      public void onChange(Person person) {
     *          RealmObject.isLoaded(person); // always true here
     *          if (RealmObject.isValid(person)) {
     *              // It is safe to access the person.
     *          }
     *      }
     * });
     * }
     * </pre>
     *
     * Synchronous RealmObjects are by definition blocking hence this method will always return {@code true} for them.
     * This method will return {@code true} if called on an unmanaged object (created outside of Realm).
     *
     * @param object RealmObject to check.
     * @return {@code true} if the query has completed, {@code false} if the query is in
     * progress.
     *
     * @see #isValid(RealmModel)
     */
public static <E extends RealmModel> boolean isLoaded(E object) {
    if (object instanceof RealmObjectProxy) {
        RealmObjectProxy proxy = (RealmObjectProxy) object;
        proxy.realmGet$proxyState().getRealm$realm().checkIfValid();
        return proxy.realmGet$proxyState().isLoaded();
    }
    return true;
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy)

Aggregations

RealmObjectProxy (io.realm.internal.RealmObjectProxy)43 Table (io.realm.internal.Table)20 Date (java.util.Date)8 Row (io.realm.internal.Row)6 InvalidRow (io.realm.internal.InvalidRow)2 LinkView (io.realm.internal.LinkView)2 UiThreadTest (android.support.test.annotation.UiThreadTest)1 AllTypes (io.realm.entities.AllTypes)1 Collection (io.realm.internal.Collection)1 PendingRow (io.realm.internal.PendingRow)1 SharedRealm (io.realm.internal.SharedRealm)1 UncheckedRow (io.realm.internal.UncheckedRow)1 Test (org.junit.Test)1 Observable (rx.Observable)1