Search in sources :

Example 1 with PendingRow

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

the class RealmQuery method findFirstAsync.

/**
     * Similar to {@link #findFirst()} but runs asynchronously on a worker thread
     * This method is only available from a Looper thread.
     *
     * @return immediately an empty {@link RealmObject}. Trying to access any field on the returned object
     * before it is loaded will throw an {@code IllegalStateException}. Use {@link RealmObject#isLoaded()} to check if
     * the object is fully loaded or register a listener {@link io.realm.RealmObject#addChangeListener}
     * to be notified when the query completes. If no RealmObject was found after the query completed, the returned
     * RealmObject will have {@link RealmObject#isLoaded()} set to {@code true} and {@link RealmObject#isValid()} set to
     * {@code false}.
     */
public E findFirstAsync() {
    realm.checkIfValid();
    realm.sharedRealm.capabilities.checkCanDeliverNotification(ASYNC_QUERY_WRONG_THREAD_MESSAGE);
    Row row;
    if (realm.isInTransaction()) {
        // It is not possible to create async query inside a transaction. So immediately query the first object.
        // See OS Results::prepare_async()
        row = new Collection(realm.sharedRealm, query).firstUncheckedRow();
    } else {
        // prepares an empty reference of the RealmObject which is backed by a pending query,
        // then update it once the query complete in the background.
        // TODO: The performance by the pending query will be a little bit worse than directly calling core's
        // Query.find(). The overhead comes with core needs to add all the row indices to the vector. However this
        // can be optimized by adding support of limit in OS's Results which is supported by core already.
        row = new PendingRow(realm.sharedRealm, query, null, isDynamicQuery());
    }
    final E result;
    if (isDynamicQuery()) {
        //noinspection unchecked
        result = (E) new DynamicRealmObject(realm, row);
    } else {
        result = realm.getConfiguration().getSchemaMediator().newInstance(clazz, realm, row, realm.getSchema().getColumnInfo(clazz), false, Collections.<String>emptyList());
    }
    if (row instanceof PendingRow) {
        final RealmObjectProxy proxy = (RealmObjectProxy) result;
        ((PendingRow) row).setFrontEnd(proxy.realmGet$proxyState());
    }
    return result;
}
Also used : RealmObjectProxy(io.realm.internal.RealmObjectProxy) Collection(io.realm.internal.Collection) Row(io.realm.internal.Row) PendingRow(io.realm.internal.PendingRow) PendingRow(io.realm.internal.PendingRow)

Aggregations

Collection (io.realm.internal.Collection)1 PendingRow (io.realm.internal.PendingRow)1 RealmObjectProxy (io.realm.internal.RealmObjectProxy)1 Row (io.realm.internal.Row)1