public static some.test.NullTypes copyOrUpdate(Realm realm, some.test.NullTypes object, boolean update, Map<RealmModel,RealmObjectProxy> cache) { if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null) { final BaseRealm otherRealm = ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm(); if (otherRealm.threadId != realm.threadId) { throw new IllegalArgumentException("Objects which belong to Realm instances in other threads cannot be copied into this Realm instance."); } if (otherRealm.getPath().equals(realm.getPath())) { return object; } } final BaseRealm.RealmObjectContext objectContext = BaseRealm.objectContext.get(); RealmObjectProxy cachedRealmObject = cache.get(object); if (cachedRealmObject != null) { return (some.test.NullTypes) cachedRealmObject; } return copy(realm, object, update, cache); }
public static some.test.Simple copyOrUpdate(Realm realm, some.test.Simple object, boolean update, Map<RealmModel,RealmObjectProxy> cache) { if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null) { final BaseRealm otherRealm = ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm(); if (otherRealm.threadId != realm.threadId) { throw new IllegalArgumentException("Objects which belong to Realm instances in other threads cannot be copied into this Realm instance."); } if (otherRealm.getPath().equals(realm.getPath())) { return object; } } final BaseRealm.RealmObjectContext objectContext = BaseRealm.objectContext.get(); RealmObjectProxy cachedRealmObject = cache.get(object); if (cachedRealmObject != null) { return (some.test.Simple) cachedRealmObject; } return copy(realm, object, update, cache); }
public static some.test.Simple copy(Realm realm, some.test.Simple newObject, boolean update, Map<RealmModel,RealmObjectProxy> cache) { RealmObjectProxy cachedRealmObject = cache.get(newObject); if (cachedRealmObject != null) { return (some.test.Simple) cachedRealmObject; } // rejecting default values to avoid creating unexpected objects from RealmModel/RealmList fields. some.test.Simple realmObject = realm.createObjectInternal(some.test.Simple.class, false, Collections.<String>emptyList()); cache.put(newObject, (RealmObjectProxy) realmObject); SimpleRealmProxyInterface realmObjectSource = (SimpleRealmProxyInterface) newObject; SimpleRealmProxyInterface realmObjectCopy = (SimpleRealmProxyInterface) realmObject; realmObjectCopy.realmSet$name(realmObjectSource.realmGet$name()); realmObjectCopy.realmSet$age(realmObjectSource.realmGet$age()); return realmObject; }
public static long insert(Realm realm, some.test.Simple 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.Simple.class); long tableNativePtr = table.getNativePtr(); SimpleColumnInfo columnInfo = (SimpleColumnInfo) realm.getSchema().getColumnInfo(some.test.Simple.class); long rowIndex = OsObject.createRow(table); cache.put(object, rowIndex); String realmGet$name = ((SimpleRealmProxyInterface) object).realmGet$name(); if (realmGet$name != null) { Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name, false); } Table.nativeSetLong(tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface) object).realmGet$age(), false); return rowIndex; }
public static void insert(Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel,Long> cache) { Table table = realm.getTable(some.test.Simple.class); long tableNativePtr = table.getNativePtr(); SimpleColumnInfo columnInfo = (SimpleColumnInfo) realm.getSchema().getColumnInfo(some.test.Simple.class); some.test.Simple object = null; while (objects.hasNext()) { object = (some.test.Simple) objects.next(); if (cache.containsKey(object)) { continue; } 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; } long rowIndex = OsObject.createRow(table); cache.put(object, rowIndex); String realmGet$name = ((SimpleRealmProxyInterface) object).realmGet$name(); if (realmGet$name != null) { Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name, false); } Table.nativeSetLong(tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface) object).realmGet$age(), false); } }
public static long insertOrUpdate(Realm realm, some.test.Simple 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.Simple.class); long tableNativePtr = table.getNativePtr(); SimpleColumnInfo columnInfo = (SimpleColumnInfo) realm.getSchema().getColumnInfo(some.test.Simple.class); long rowIndex = OsObject.createRow(table); cache.put(object, rowIndex); String realmGet$name = ((SimpleRealmProxyInterface) object).realmGet$name(); if (realmGet$name != null) { Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name, false); } else { Table.nativeSetNull(tableNativePtr, columnInfo.nameIndex, rowIndex, false); } Table.nativeSetLong(tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface) object).realmGet$age(), false); return rowIndex; }
public static void insertOrUpdate(Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel,Long> cache) { Table table = realm.getTable(some.test.Simple.class); long tableNativePtr = table.getNativePtr(); SimpleColumnInfo columnInfo = (SimpleColumnInfo) realm.getSchema().getColumnInfo(some.test.Simple.class); some.test.Simple object = null; while (objects.hasNext()) { object = (some.test.Simple) objects.next(); if (cache.containsKey(object)) { continue; } 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; } long rowIndex = OsObject.createRow(table); cache.put(object, rowIndex); String realmGet$name = ((SimpleRealmProxyInterface) object).realmGet$name(); if (realmGet$name != null) { Table.nativeSetString(tableNativePtr, columnInfo.nameIndex, rowIndex, realmGet$name, false); } else { Table.nativeSetNull(tableNativePtr, columnInfo.nameIndex, rowIndex, false); } Table.nativeSetLong(tableNativePtr, columnInfo.ageIndex, rowIndex, ((SimpleRealmProxyInterface) object).realmGet$age(), false); } }
public static some.test.Simple createDetachedCopy(some.test.Simple realmObject, int currentDepth, int maxDepth, Map<RealmModel, CacheData<RealmModel>> cache) { if (currentDepth > maxDepth || realmObject == null) { return null; } CacheData<RealmModel> cachedObject = cache.get(realmObject); some.test.Simple unmanagedObject; if (cachedObject == null) { unmanagedObject = new some.test.Simple(); cache.put(realmObject, new RealmObjectProxy.CacheData<RealmModel>(currentDepth, unmanagedObject)); } else { // Reuse cached object or recreate it because it was encountered at a lower depth. if (currentDepth >= cachedObject.minDepth) { return (some.test.Simple) cachedObject.object; } unmanagedObject = (some.test.Simple) cachedObject.object; cachedObject.minDepth = currentDepth; } SimpleRealmProxyInterface unmanagedCopy = (SimpleRealmProxyInterface) unmanagedObject; SimpleRealmProxyInterface realmSource = (SimpleRealmProxyInterface) realmObject; unmanagedCopy.realmSet$name(realmSource.realmGet$name()); unmanagedCopy.realmSet$age(realmSource.realmGet$age()); return unmanagedObject; }
@Override public void insert(Realm realm, Collection<? extends RealmModel> objects) { Iterator<? extends RealmModel> iterator = objects.iterator(); RealmModel object = null; Map<RealmModel, Long> cache = new HashMap<RealmModel, Long>(objects.size()); if (iterator.hasNext()) { // access the first element to figure out the clazz for the routing below object = iterator.next(); // This cast is correct because obj is either // generated by RealmProxy or the original type extending directly from RealmObject @SuppressWarnings("unchecked") Class<RealmModel> clazz = (Class<RealmModel>) ((object instanceof RealmObjectProxy) ? object.getClass().getSuperclass() : object.getClass()); if (clazz.equals(some.test.AllTypes.class)) { io.realm.AllTypesRealmProxy.insert(realm, (some.test.AllTypes) object, cache); } else { throw getMissingProxyClassException(clazz); } if (iterator.hasNext()) { if (clazz.equals(some.test.AllTypes.class)) { io.realm.AllTypesRealmProxy.insert(realm, iterator, cache); } else { throw getMissingProxyClassException(clazz); } } } }
@Override public void insertOrUpdate(Realm realm, Collection<? extends RealmModel> objects) { Iterator<? extends RealmModel> iterator = objects.iterator(); RealmModel object = null; Map<RealmModel, Long> cache = new HashMap<RealmModel, Long>(objects.size()); if (iterator.hasNext()) { // access the first element to figure out the clazz for the routing below object = iterator.next(); // This cast is correct because obj is either // generated by RealmProxy or the original type extending directly from RealmObject @SuppressWarnings("unchecked") Class<RealmModel> clazz = (Class<RealmModel>) ((object instanceof RealmObjectProxy) ? object.getClass().getSuperclass() : object.getClass()); if (clazz.equals(some.test.AllTypes.class)) { io.realm.AllTypesRealmProxy.insertOrUpdate(realm, (some.test.AllTypes) object, cache); } else { throw getMissingProxyClassException(clazz); } if (iterator.hasNext()) { if (clazz.equals(some.test.AllTypes.class)) { io.realm.AllTypesRealmProxy.insertOrUpdate(realm, iterator, cache); } else { throw getMissingProxyClassException(clazz); } } } }
public static some.test.Booleans copyOrUpdate(Realm realm, some.test.Booleans object, boolean update, Map<RealmModel,RealmObjectProxy> cache) { if (object instanceof RealmObjectProxy && ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm() != null) { final BaseRealm otherRealm = ((RealmObjectProxy) object).realmGet$proxyState().getRealm$realm(); if (otherRealm.threadId != realm.threadId) { throw new IllegalArgumentException("Objects which belong to Realm instances in other threads cannot be copied into this Realm instance."); } if (otherRealm.getPath().equals(realm.getPath())) { return object; } } final BaseRealm.RealmObjectContext objectContext = BaseRealm.objectContext.get(); RealmObjectProxy cachedRealmObject = cache.get(object); if (cachedRealmObject != null) { return (some.test.Booleans) cachedRealmObject; } return copy(realm, object, update, cache); }
public static some.test.Booleans copy(Realm realm, some.test.Booleans newObject, boolean update, Map<RealmModel,RealmObjectProxy> cache) { RealmObjectProxy cachedRealmObject = cache.get(newObject); if (cachedRealmObject != null) { return (some.test.Booleans) cachedRealmObject; } // rejecting default values to avoid creating unexpected objects from RealmModel/RealmList fields. some.test.Booleans realmObject = realm.createObjectInternal(some.test.Booleans.class, false, Collections.<String>emptyList()); cache.put(newObject, (RealmObjectProxy) realmObject); BooleansRealmProxyInterface realmObjectSource = (BooleansRealmProxyInterface) newObject; BooleansRealmProxyInterface realmObjectCopy = (BooleansRealmProxyInterface) realmObject; realmObjectCopy.realmSet$done(realmObjectSource.realmGet$done()); realmObjectCopy.realmSet$isReady(realmObjectSource.realmGet$isReady()); realmObjectCopy.realmSet$mCompleted(realmObjectSource.realmGet$mCompleted()); realmObjectCopy.realmSet$anotherBoolean(realmObjectSource.realmGet$anotherBoolean()); return realmObject; }
public static void insert(Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel,Long> cache) { Table table = realm.getTable(some.test.Booleans.class); long tableNativePtr = table.getNativePtr(); BooleansColumnInfo columnInfo = (BooleansColumnInfo) realm.getSchema().getColumnInfo(some.test.Booleans.class); some.test.Booleans object = null; while (objects.hasNext()) { object = (some.test.Booleans) objects.next(); if (cache.containsKey(object)) { continue; } 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; } long rowIndex = OsObject.createRow(table); cache.put(object, rowIndex); Table.nativeSetBoolean(tableNativePtr, columnInfo.doneIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$done(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.isReadyIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$isReady(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.mCompletedIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$mCompleted(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.anotherBooleanIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$anotherBoolean(), false); } }
public static void insertOrUpdate(Realm realm, Iterator<? extends RealmModel> objects, Map<RealmModel,Long> cache) { Table table = realm.getTable(some.test.Booleans.class); long tableNativePtr = table.getNativePtr(); BooleansColumnInfo columnInfo = (BooleansColumnInfo) realm.getSchema().getColumnInfo(some.test.Booleans.class); some.test.Booleans object = null; while (objects.hasNext()) { object = (some.test.Booleans) objects.next(); if (cache.containsKey(object)) { continue; } 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; } long rowIndex = OsObject.createRow(table); cache.put(object, rowIndex); Table.nativeSetBoolean(tableNativePtr, columnInfo.doneIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$done(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.isReadyIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$isReady(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.mCompletedIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$mCompleted(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.anotherBooleanIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$anotherBoolean(), false); } }
public static some.test.Booleans createDetachedCopy(some.test.Booleans realmObject, int currentDepth, int maxDepth, Map<RealmModel, CacheData<RealmModel>> cache) { if (currentDepth > maxDepth || realmObject == null) { return null; } CacheData<RealmModel> cachedObject = cache.get(realmObject); some.test.Booleans unmanagedObject; if (cachedObject == null) { unmanagedObject = new some.test.Booleans(); cache.put(realmObject, new RealmObjectProxy.CacheData<RealmModel>(currentDepth, unmanagedObject)); } else { // Reuse cached object or recreate it because it was encountered at a lower depth. if (currentDepth >= cachedObject.minDepth) { return (some.test.Booleans) cachedObject.object; } unmanagedObject = (some.test.Booleans) cachedObject.object; cachedObject.minDepth = currentDepth; } BooleansRealmProxyInterface unmanagedCopy = (BooleansRealmProxyInterface) unmanagedObject; BooleansRealmProxyInterface realmSource = (BooleansRealmProxyInterface) realmObject; unmanagedCopy.realmSet$done(realmSource.realmGet$done()); unmanagedCopy.realmSet$isReady(realmSource.realmGet$isReady()); unmanagedCopy.realmSet$mCompleted(realmSource.realmGet$mCompleted()); unmanagedCopy.realmSet$anotherBoolean(realmSource.realmGet$anotherBoolean()); return unmanagedObject; }
/** * Searches this {@link OrderedRealmCollection} for the specified object. * * @param object the object to search for. * @return {@code true} if {@code object} is an element of this {@code OrderedRealmCollection}, * {@code false} otherwise. */ @Override public boolean contains(@Nullable Object object) { if (isLoaded()) { // Deleted objects can never be part of a RealmResults if (object instanceof RealmObjectProxy) { RealmObjectProxy proxy = (RealmObjectProxy) object; if (proxy.realmGet$proxyState().getRow$realm() == InvalidRow.INSTANCE) { return false; } } for (E e : this) { if (e.equals(object)) { return true; } } } return false; }
/** * Returns {@code true} if the list contains the specified element when attached to a Realm. This * method will query the native Realm underlying storage engine to quickly find the specified element. * <p> * If the list is not attached to a Realm, the default {@link List#contains(Object)} * implementation will occur. * * @param object the element whose presence in this list is to be tested. * @return {@code true} if this list contains the specified element otherwise {@code false}. */ @Override public boolean contains(@Nullable Object object) { if (isManaged()) { realm.checkIfValid(); // Deleted objects can never be part of a RealmList if (object instanceof RealmObjectProxy) { RealmObjectProxy proxy = (RealmObjectProxy) object; if (proxy.realmGet$proxyState().getRow$realm() == InvalidRow.INSTANCE) { return false; } } return super.contains(object); } else { return unmanagedList.contains(object); } }
/** * Updates a list of existing RealmObjects that is identified by their {@link io.realm.annotations.PrimaryKey} or * creates a new copy if no existing object could be found. This is a deep copy or update i.e., all referenced * objects will be either copied or updated. * <p> * Please note, copying an object will copy all field values. Any unset field in the objects and child objects will be * set to their default value if not provided. * * @param objects a list of objects to update or copy into Realm. * @return a list of all the new or updated RealmObjects. * @throws java.lang.IllegalArgumentException if RealmObject is {@code null} or doesn't have a Primary key defined. * @see #copyToRealm(Iterable) */ public <E extends RealmModel> List<E> copyToRealmOrUpdate(Iterable<E> objects) { //noinspection ConstantConditions if (objects == null) { return new ArrayList<>(0); } Map<RealmModel, RealmObjectProxy> cache = new HashMap<>(); ArrayList<E> realmObjects = new ArrayList<>(); for (E object : objects) { checkNotNullObject(object); realmObjects.add(copyOrUpdate(object, true, cache)); } return realmObjects; }
/** * Creates a dynamic Realm object based on an existing object. * * @param obj the Realm object to convert to a dynamic object. Only objects managed by {@link Realm} can be used. * @throws IllegalArgumentException if object isn't managed by Realm or is a {@link DynamicRealmObject} already. */ public DynamicRealmObject(RealmModel obj) { //noinspection ConstantConditions if (obj == null) { throw new IllegalArgumentException("A non-null object must be provided."); } if (obj instanceof DynamicRealmObject) { throw new IllegalArgumentException("The object is already a DynamicRealmObject: " + obj); } if (!RealmObject.isManaged(obj)) { throw new IllegalArgumentException("An object managed by Realm must be provided. This " + "is an unmanaged object."); } if (!RealmObject.isValid(obj)) { throw new IllegalArgumentException("A valid object managed by Realm must be provided. " + "This object was deleted."); } RealmObjectProxy proxy = (RealmObjectProxy) obj; Row row = proxy.realmGet$proxyState().getRow$realm(); proxyState.setRealm$realm(proxy.realmGet$proxyState().getRealm$realm()); proxyState.setRow$realm(((UncheckedRow) row).convertToChecked()); proxyState.setConstructionFinished(); }
/** * 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); }
/** * returns {@link Realm} instance where the {@code model} belongs. * <p> * You <b>must not</b> call {@link Realm#close()} against returned instance. * * @param model an {@link RealmModel} instance other than {@link DynamicRealmObject}. * @return {@link Realm} instance where the {@code model} belongs or {@code null} if the {@code model} is unmanaged. * @throws IllegalArgumentException if the {@code model} is {@code null}. * @throws IllegalStateException if the {@code model} is an instance of {@link DynamicRealmObject} * or this object was already deleted or the corresponding {@link Realm} was already closed. */ public static Realm getRealm(RealmModel model) { if (model == null) { throw new IllegalArgumentException(MSG_NULL_OBJECT); } if (model instanceof DynamicRealmObject) { throw new IllegalStateException(MSG_DYNAMIC_OBJECT); } if (!(model instanceof RealmObjectProxy)) { return null; } final BaseRealm realm = ((RealmObjectProxy) model).realmGet$proxyState().getRealm$realm(); realm.checkIfValid(); if (!RealmObject.isValid(model)) { throw new IllegalStateException(MSG_DELETED_OBJECT); } return (Realm) realm; }
/** * 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, RealmObjectChangeListener listener) { //noinspection ConstantConditions if (object == null) { throw new IllegalArgumentException("Object should not be null"); } //noinspection ConstantConditions 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)"); } }
/** * Returns an Rx Observable that monitors changes to this RealmObject. It will emit the current RealmObject when * subscribed to. For each update to the RealmObject a pair consisting of the RealmObject and the * {@link ObjectChangeSet} will be sent. The changeset will be {@code null} the first * time the RealmObject is emitted. * <p> * The RealmObject will continually be emitted as it is updated - {@code onComplete} will never be called. * <p> * Note that when the {@link Realm} is accessed from threads other than where it was created, * {@link IllegalStateException} will be thrown. Care should be taken when using different schedulers * with {@code subscribeOn()} and {@code observeOn()}. Consider using {@code Realm.where().find*Async()} * instead. * * @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 or the * corresponding Realm instance doesn't support RxJava. * @see <a href="https://realm.io/docs/java/latest/#rxjava">RxJava and Realm</a> */ public static <E extends RealmModel> Observable<ObjectChange<E>> asChangesetObservable(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().changesetsFrom((Realm) realm, object); } else if (realm instanceof DynamicRealm) { DynamicRealm dynamicRealm = (DynamicRealm) realm; DynamicRealmObject dynamicObject = (DynamicRealmObject) object; return (Observable) realm.configuration.getRxFactory().changesetsFrom(dynamicRealm, dynamicObject); } 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"); } }
private String getRealmRowIdentifier(int realmIndex, HashMap<Long, RealmFieldType> columnIndexRealmFieldTypeHashMap) { String rowIdentifier = ""; RealmObjectProxy proxy = (RealmObjectProxy) adapterData.get(realmIndex); Row row = proxy.realmGet$proxyState().getRow$realm(); for (Entry<Long, RealmFieldType> entry : columnIndexRealmFieldTypeHashMap.entrySet()) { switch (entry.getValue()) { case STRING: rowIdentifier += row.getString(entry.getKey()); break; case INTEGER: rowIdentifier += String.valueOf(row.getLong(entry.getKey())); break; case BOOLEAN: rowIdentifier += String.valueOf(row.getBoolean(entry.getKey())); break; default: throw new IllegalStateException("Unsupported RealmFieldType, use only STRING, INTEGER or BOOLEAN field types"); } } return rowIdentifier; }
@Override public void realmSet$columnObject(some.test.AllTypes value) { if (proxyState.isUnderConstruction()) { if (!proxyState.getAcceptDefaultValue$realm()) { return; } if (proxyState.getExcludeFields$realm().contains("columnObject")) { return; } if (value != null && !RealmObject.isManaged(value)) { value = ((Realm) proxyState.getRealm$realm()).copyToRealm(value); } final Row row = proxyState.getRow$realm(); if (value == null) { // Table#nullifyLink() does not support default value. Just using Row. row.nullifyLink(columnInfo.columnObjectIndex); return; } proxyState.checkValidObject(value); row.getTable().setLink(columnInfo.columnObjectIndex, row.getIndex(), ((RealmObjectProxy) value).realmGet$proxyState().getRow$realm().getIndex(), true); return; } proxyState.getRealm$realm().checkIfValid(); if (value == null) { proxyState.getRow$realm().nullifyLink(columnInfo.columnObjectIndex); return; } proxyState.checkValidObject(value); proxyState.getRow$realm().setLink(columnInfo.columnObjectIndex, ((RealmObjectProxy) value).realmGet$proxyState().getRow$realm().getIndex()); }
@Override public void realmSet$fieldObjectNull(some.test.NullTypes value) { if (proxyState.isUnderConstruction()) { if (!proxyState.getAcceptDefaultValue$realm()) { return; } if (proxyState.getExcludeFields$realm().contains("fieldObjectNull")) { return; } if (value != null && !RealmObject.isManaged(value)) { value = ((Realm) proxyState.getRealm$realm()).copyToRealm(value); } final Row row = proxyState.getRow$realm(); if (value == null) { // Table#nullifyLink() does not support default value. Just using Row. row.nullifyLink(columnInfo.fieldObjectNullIndex); return; } proxyState.checkValidObject(value); row.getTable().setLink(columnInfo.fieldObjectNullIndex, row.getIndex(), ((RealmObjectProxy) value).realmGet$proxyState().getRow$realm().getIndex(), true); return; } proxyState.getRealm$realm().checkIfValid(); if (value == null) { proxyState.getRow$realm().nullifyLink(columnInfo.fieldObjectNullIndex); return; } proxyState.checkValidObject(value); proxyState.getRow$realm().setLink(columnInfo.fieldObjectNullIndex, ((RealmObjectProxy) value).realmGet$proxyState().getRow$realm().getIndex()); }
@Override public <E extends RealmModel> E copyOrUpdate(Realm realm, E obj, boolean update, Map<RealmModel, RealmObjectProxy> cache) { // This cast is correct because obj is either // generated by RealmProxy or the original type extending directly from RealmObject @SuppressWarnings("unchecked") Class<E> clazz = (Class<E>) ((obj instanceof RealmObjectProxy) ? obj.getClass().getSuperclass() : obj.getClass()); if (clazz.equals(some.test.AllTypes.class)) { return clazz.cast(io.realm.AllTypesRealmProxy.copyOrUpdate(realm, (some.test.AllTypes) obj, update, cache)); } throw getMissingProxyClassException(clazz); }
@Override public void insert(Realm realm, RealmModel object, Map<RealmModel, Long> cache) { // This cast is correct because obj is either // generated by RealmProxy or the original type extending directly from RealmObject @SuppressWarnings("unchecked") Class<RealmModel> clazz = (Class<RealmModel>) ((object instanceof RealmObjectProxy) ? object.getClass().getSuperclass() : object.getClass()); if (clazz.equals(some.test.AllTypes.class)) { io.realm.AllTypesRealmProxy.insert(realm, (some.test.AllTypes) object, cache); } else { throw getMissingProxyClassException(clazz); } }
@Override public void insertOrUpdate(Realm realm, RealmModel obj, Map<RealmModel, Long> cache) { // This cast is correct because obj is either // generated by RealmProxy or the original type extending directly from RealmObject @SuppressWarnings("unchecked") Class<RealmModel> clazz = (Class<RealmModel>) ((obj instanceof RealmObjectProxy) ? obj.getClass().getSuperclass() : obj.getClass()); if (clazz.equals(some.test.AllTypes.class)) { io.realm.AllTypesRealmProxy.insertOrUpdate(realm, (some.test.AllTypes) obj, cache); } else { throw getMissingProxyClassException(clazz); } }
@Override public <E extends RealmModel> E createDetachedCopy(E realmObject, int maxDepth, Map<RealmModel, RealmObjectProxy.CacheData<RealmModel>> cache) { // This cast is correct because obj is either // generated by RealmProxy or the original type extending directly from RealmObject @SuppressWarnings("unchecked") Class<E> clazz = (Class<E>) realmObject.getClass().getSuperclass(); if (clazz.equals(some.test.AllTypes.class)) { return clazz.cast(io.realm.AllTypesRealmProxy.createDetachedCopy((some.test.AllTypes) realmObject, 0, maxDepth, cache)); } throw getMissingProxyClassException(clazz); }
public static long insert(Realm realm, some.test.Booleans 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.Booleans.class); long tableNativePtr = table.getNativePtr(); BooleansColumnInfo columnInfo = (BooleansColumnInfo) realm.getSchema().getColumnInfo(some.test.Booleans.class); long rowIndex = OsObject.createRow(table); cache.put(object, rowIndex); Table.nativeSetBoolean(tableNativePtr, columnInfo.doneIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$done(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.isReadyIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$isReady(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.mCompletedIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$mCompleted(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.anotherBooleanIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$anotherBoolean(), false); return rowIndex; }
public static long insertOrUpdate(Realm realm, some.test.Booleans 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.Booleans.class); long tableNativePtr = table.getNativePtr(); BooleansColumnInfo columnInfo = (BooleansColumnInfo) realm.getSchema().getColumnInfo(some.test.Booleans.class); long rowIndex = OsObject.createRow(table); cache.put(object, rowIndex); Table.nativeSetBoolean(tableNativePtr, columnInfo.doneIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$done(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.isReadyIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$isReady(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.mCompletedIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$mCompleted(), false); Table.nativeSetBoolean(tableNativePtr, columnInfo.anotherBooleanIndex, rowIndex, ((BooleansRealmProxyInterface) object).realmGet$anotherBoolean(), false); return rowIndex; }
@Override public void insertValue(int index, Object value) { // need to check in advance to avoid unnecessary copy of unmanaged object into Realm. checkInsertIndex(index); RealmObjectProxy proxy = (RealmObjectProxy) copyToRealmIfNeeded((RealmModel) value); osList.insertRow(index, proxy.realmGet$proxyState().getRow$realm().getIndex()); }
/** * Copies a collection of RealmObjects to the Realm instance and returns their copy. Any further changes to the * original RealmObjects will not be reflected in the Realm copies. This is a deep copy i.e., all referenced objects * will be copied. Objects already in this Realm will be ignored. * <p> * Please note, copying an object will copy all field values. Any unset field in the objects and child objects will be * set to their default value if not provided. * * @param objects the RealmObjects to copy to the Realm. * @return a list of the the converted RealmObjects that all has their properties managed by the Realm. * @throws io.realm.exceptions.RealmException if any of the objects has already been added to Realm. * @throws java.lang.IllegalArgumentException if any of the elements in the input collection is {@code null}. */ public <E extends RealmModel> List<E> copyToRealm(Iterable<E> objects) { //noinspection ConstantConditions if (objects == null) { return new ArrayList<>(); } Map<RealmModel, RealmObjectProxy> cache = new HashMap<>(); ArrayList<E> realmObjects = new ArrayList<>(); for (E object : objects) { checkNotNullObject(object); realmObjects.add(copyOrUpdate(object, false, cache)); } return realmObjects; }
/** * Check that object is a valid and managed object by this Realm. * Used by proxy classes to verify input. * * @param value model object */ public void checkValidObject(RealmModel value) { if (!RealmObject.isValid(value) || !RealmObject.isManaged(value)) { throw new IllegalArgumentException("'value' is not a valid managed object."); } if (((RealmObjectProxy) value).realmGet$proxyState().getRealm$realm() != getRealm$realm()) { throw new IllegalArgumentException("'value' belongs to a different Realm."); } }
/** * 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; } }
/** * Makes an asynchronous query blocking. This will also trigger any registered listeners. * <p> * Note: This will return {@code true} if called for an unmanaged object (created outside of Realm). * * @param object RealmObject to force load. * @return {@code true} if it successfully completed the query, {@code false} otherwise. */ public static <E extends RealmModel> boolean load(E object) { if (RealmObject.isLoaded(object)) { return true; } else if (object instanceof RealmObjectProxy) { ((RealmObjectProxy) object).realmGet$proxyState().load(); return true; } return false; }
/** * 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)"); } }
@Test public void toString_managedMode() { StringBuilder sb = new StringBuilder("RealmList<Dog>@["); for (int i = 0; i < collection.size() - 1; i++) { sb.append(((RealmObjectProxy) (collection.get(i))).realmGet$proxyState().getRow$realm().getIndex()); sb.append(","); } sb.append(((RealmObjectProxy)collection.get(TEST_SIZE - 1)).realmGet$proxyState().getRow$realm().getIndex()); sb.append("]"); assertEquals(sb.toString(), collection.toString()); }
@Test public void row_isValid() { realm.beginTransaction(); RealmObjectProxy realmObject = (RealmObjectProxy) realm.createObject(AllTypes.class); Row row = realmObject.realmGet$proxyState().getRow$realm(); realm.commitTransaction(); assertNotNull("RealmObject.realmGetRow returns zero ", row); assertEquals(17, row.getColumnCount()); }