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;
}
use of io.realm.internal.RealmObjectProxy 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;
}
use of io.realm.internal.RealmObjectProxy in project realm-java by realm.
the class AllTypesRealmProxy method copy.
public static some.test.AllTypes copy(Realm realm, some.test.AllTypes newObject, boolean update, Map<RealmModel, RealmObjectProxy> cache) {
RealmObjectProxy cachedRealmObject = cache.get(newObject);
if (cachedRealmObject != null) {
return (some.test.AllTypes) cachedRealmObject;
} else {
// rejecting default values to avoid creating unexpected objects from RealmModel/RealmList fields.
some.test.AllTypes realmObject = realm.createObjectInternal(some.test.AllTypes.class, ((AllTypesRealmProxyInterface) newObject).realmGet$columnString(), false, Collections.<String>emptyList());
cache.put(newObject, (RealmObjectProxy) realmObject);
((AllTypesRealmProxyInterface) realmObject).realmSet$columnLong(((AllTypesRealmProxyInterface) newObject).realmGet$columnLong());
((AllTypesRealmProxyInterface) realmObject).realmSet$columnFloat(((AllTypesRealmProxyInterface) newObject).realmGet$columnFloat());
((AllTypesRealmProxyInterface) realmObject).realmSet$columnDouble(((AllTypesRealmProxyInterface) newObject).realmGet$columnDouble());
((AllTypesRealmProxyInterface) realmObject).realmSet$columnBoolean(((AllTypesRealmProxyInterface) newObject).realmGet$columnBoolean());
((AllTypesRealmProxyInterface) realmObject).realmSet$columnDate(((AllTypesRealmProxyInterface) newObject).realmGet$columnDate());
((AllTypesRealmProxyInterface) realmObject).realmSet$columnBinary(((AllTypesRealmProxyInterface) newObject).realmGet$columnBinary());
some.test.AllTypes columnObjectObj = ((AllTypesRealmProxyInterface) newObject).realmGet$columnObject();
if (columnObjectObj != null) {
some.test.AllTypes cachecolumnObject = (some.test.AllTypes) cache.get(columnObjectObj);
if (cachecolumnObject != null) {
((AllTypesRealmProxyInterface) realmObject).realmSet$columnObject(cachecolumnObject);
} else {
((AllTypesRealmProxyInterface) realmObject).realmSet$columnObject(AllTypesRealmProxy.copyOrUpdate(realm, columnObjectObj, update, cache));
}
} else {
((AllTypesRealmProxyInterface) realmObject).realmSet$columnObject(null);
}
RealmList<some.test.AllTypes> columnRealmListList = ((AllTypesRealmProxyInterface) newObject).realmGet$columnRealmList();
if (columnRealmListList != null) {
RealmList<some.test.AllTypes> columnRealmListRealmList = ((AllTypesRealmProxyInterface) realmObject).realmGet$columnRealmList();
for (int i = 0; i < columnRealmListList.size(); i++) {
some.test.AllTypes columnRealmListItem = columnRealmListList.get(i);
some.test.AllTypes cachecolumnRealmList = (some.test.AllTypes) cache.get(columnRealmListItem);
if (cachecolumnRealmList != null) {
columnRealmListRealmList.add(cachecolumnRealmList);
} else {
columnRealmListRealmList.add(AllTypesRealmProxy.copyOrUpdate(realm, columnRealmListList.get(i), update, cache));
}
}
}
return realmObject;
}
}
use of io.realm.internal.RealmObjectProxy in project realm-java by realm.
the class AllTypesRealmProxy method insert.
public static long insert(Realm realm, some.test.AllTypes object, Map<RealmModel, Long> cache) {
if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) {
return ((RealmObjectProxy) object).realmGet$proxyState().getRow$realm().getIndex();
}
Table table = realm.getTable(some.test.AllTypes.class);
long tableNativePtr = table.getNativeTablePointer();
AllTypesColumnInfo columnInfo = (AllTypesColumnInfo) realm.schema.getColumnInfo(some.test.AllTypes.class);
long pkColumnIndex = table.getPrimaryKey();
String primaryKeyValue = ((AllTypesRealmProxyInterface) object).realmGet$columnString();
long rowIndex = Table.NO_MATCH;
if (primaryKeyValue == null) {
rowIndex = Table.nativeFindFirstNull(tableNativePtr, pkColumnIndex);
} else {
rowIndex = Table.nativeFindFirstString(tableNativePtr, pkColumnIndex, primaryKeyValue);
}
if (rowIndex == Table.NO_MATCH) {
rowIndex = table.addEmptyRowWithPrimaryKey(primaryKeyValue, false);
} else {
Table.throwDuplicatePrimaryKeyException(primaryKeyValue);
}
cache.put(object, rowIndex);
Table.nativeSetLong(tableNativePtr, columnInfo.columnLongIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnLong(), false);
Table.nativeSetFloat(tableNativePtr, columnInfo.columnFloatIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnFloat(), false);
Table.nativeSetDouble(tableNativePtr, columnInfo.columnDoubleIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnDouble(), false);
Table.nativeSetBoolean(tableNativePtr, columnInfo.columnBooleanIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnBoolean(), false);
java.util.Date realmGet$columnDate = ((AllTypesRealmProxyInterface) object).realmGet$columnDate();
if (realmGet$columnDate != null) {
Table.nativeSetTimestamp(tableNativePtr, columnInfo.columnDateIndex, rowIndex, realmGet$columnDate.getTime(), false);
}
byte[] realmGet$columnBinary = ((AllTypesRealmProxyInterface) object).realmGet$columnBinary();
if (realmGet$columnBinary != null) {
Table.nativeSetByteArray(tableNativePtr, columnInfo.columnBinaryIndex, rowIndex, realmGet$columnBinary, false);
}
some.test.AllTypes columnObjectObj = ((AllTypesRealmProxyInterface) object).realmGet$columnObject();
if (columnObjectObj != null) {
Long cachecolumnObject = cache.get(columnObjectObj);
if (cachecolumnObject == null) {
cachecolumnObject = AllTypesRealmProxy.insert(realm, columnObjectObj, cache);
}
Table.nativeSetLink(tableNativePtr, columnInfo.columnObjectIndex, rowIndex, cachecolumnObject, false);
}
RealmList<some.test.AllTypes> columnRealmListList = ((AllTypesRealmProxyInterface) object).realmGet$columnRealmList();
if (columnRealmListList != null) {
long columnRealmListNativeLinkViewPtr = Table.nativeGetLinkView(tableNativePtr, columnInfo.columnRealmListIndex, rowIndex);
for (some.test.AllTypes columnRealmListItem : columnRealmListList) {
Long cacheItemIndexcolumnRealmList = cache.get(columnRealmListItem);
if (cacheItemIndexcolumnRealmList == null) {
cacheItemIndexcolumnRealmList = AllTypesRealmProxy.insert(realm, columnRealmListItem, cache);
}
LinkView.nativeAdd(columnRealmListNativeLinkViewPtr, cacheItemIndexcolumnRealmList);
}
}
return rowIndex;
}
use of io.realm.internal.RealmObjectProxy in project realm-java by realm.
the class AllTypesRealmProxy method insert.
public static void insert(Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel, Long> cache) {
Table table = realm.getTable(some.test.AllTypes.class);
long tableNativePtr = table.getNativeTablePointer();
AllTypesColumnInfo columnInfo = (AllTypesColumnInfo) realm.schema.getColumnInfo(some.test.AllTypes.class);
long pkColumnIndex = table.getPrimaryKey();
some.test.AllTypes object = null;
while (objects.hasNext()) {
object = (some.test.AllTypes) objects.next();
if (!cache.containsKey(object)) {
if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm().getPath().equals(realm.getPath())) {
cache.put(object, ((RealmObjectProxy) object).realmGet$proxyState().getRow$realm().getIndex());
continue;
}
String primaryKeyValue = ((AllTypesRealmProxyInterface) object).realmGet$columnString();
long rowIndex = Table.NO_MATCH;
if (primaryKeyValue == null) {
rowIndex = Table.nativeFindFirstNull(tableNativePtr, pkColumnIndex);
} else {
rowIndex = Table.nativeFindFirstString(tableNativePtr, pkColumnIndex, primaryKeyValue);
}
if (rowIndex == Table.NO_MATCH) {
rowIndex = table.addEmptyRowWithPrimaryKey(primaryKeyValue, false);
} else {
Table.throwDuplicatePrimaryKeyException(primaryKeyValue);
}
cache.put(object, rowIndex);
Table.nativeSetLong(tableNativePtr, columnInfo.columnLongIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnLong(), false);
Table.nativeSetFloat(tableNativePtr, columnInfo.columnFloatIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnFloat(), false);
Table.nativeSetDouble(tableNativePtr, columnInfo.columnDoubleIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnDouble(), false);
Table.nativeSetBoolean(tableNativePtr, columnInfo.columnBooleanIndex, rowIndex, ((AllTypesRealmProxyInterface) object).realmGet$columnBoolean(), false);
java.util.Date realmGet$columnDate = ((AllTypesRealmProxyInterface) object).realmGet$columnDate();
if (realmGet$columnDate != null) {
Table.nativeSetTimestamp(tableNativePtr, columnInfo.columnDateIndex, rowIndex, realmGet$columnDate.getTime(), false);
}
byte[] realmGet$columnBinary = ((AllTypesRealmProxyInterface) object).realmGet$columnBinary();
if (realmGet$columnBinary != null) {
Table.nativeSetByteArray(tableNativePtr, columnInfo.columnBinaryIndex, rowIndex, realmGet$columnBinary, false);
}
some.test.AllTypes columnObjectObj = ((AllTypesRealmProxyInterface) object).realmGet$columnObject();
if (columnObjectObj != null) {
Long cachecolumnObject = cache.get(columnObjectObj);
if (cachecolumnObject == null) {
cachecolumnObject = AllTypesRealmProxy.insert(realm, columnObjectObj, cache);
}
table.setLink(columnInfo.columnObjectIndex, rowIndex, cachecolumnObject, false);
}
RealmList<some.test.AllTypes> columnRealmListList = ((AllTypesRealmProxyInterface) object).realmGet$columnRealmList();
if (columnRealmListList != null) {
long columnRealmListNativeLinkViewPtr = Table.nativeGetLinkView(tableNativePtr, columnInfo.columnRealmListIndex, rowIndex);
for (some.test.AllTypes columnRealmListItem : columnRealmListList) {
Long cacheItemIndexcolumnRealmList = cache.get(columnRealmListItem);
if (cacheItemIndexcolumnRealmList == null) {
cacheItemIndexcolumnRealmList = AllTypesRealmProxy.insert(realm, columnRealmListItem, cache);
}
LinkView.nativeAdd(columnRealmListNativeLinkViewPtr, cacheItemIndexcolumnRealmList);
}
}
}
}
}
Aggregations