Package io.objectbox.relation
Class ToMany<TARGET>
java.lang.Object
io.objectbox.relation.ToMany<TARGET>
- Type Parameters:
TARGET- Object type (entity).
- All Implemented Interfaces:
Serializable,Iterable<TARGET>,Collection<TARGET>,List<TARGET>
A List representing a to-many relation.
It tracks changes (adds and removes) that can be later applied (persisted) to the database.
This happens either on
Box.put(Object) of the source entity of this relation or using
applyChangesToDb().
If this relation is a backlink from a ToOne relation, a DB sync will also update ToOne objects
(but not vice versa).
ToMany is thread-safe by default (only if the default CopyOnWriteArrayList is used).
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidSeeadd(Object)for general comments.booleanAdds the given entity to the list and tracks the addition so it can be later applied to the database (e.g.booleanaddAll(int index, Collection<? extends TARGET> objects) Seeadd(Object)for general comments.booleanaddAll(Collection<? extends TARGET> objects) Seeadd(Object)for general comments.voidApplies (persists) tracked changes (added and removed entities) to the target box and/or updates standalone relations.voidclear()booleanbooleancontainsAll(Collection<?> collection) get(int location) intgetById(long id) Gets an object by its entity ID.intbooleanhasA(QueryFilter<TARGET> filter) Returns true if at least one of the entities matches the given filter.booleanhasAll(QueryFilter<TARGET> filter) Returns true if all of the entities match the given filter.booleanReturns true if there are pending changes for the DB.intintindexOfId(long id) Gets the index of the object with the given entity ID.voidinternalApplyToDb(io.objectbox.Cursor<?> sourceCursor, io.objectbox.Cursor<TARGET> targetCursor) For internal use only; do not use in your app.booleanFor internal use only; do not use in your app.booleanisEmpty()booleaniterator()intlastIndexOf(Object object) listIterator(int location) The returned iterator does not track any potential calls toIterator.remove().remove(int location) booleanbooleanremoveAll(Collection<?> objects) removeById(long id) Removes an object by its entity ID.voidreset()Resets the already loaded entities so they will be re-loaded on their next access.booleanretainAll(Collection<?> objects) voidsetComparator(Comparator<TARGET> comparator) Set an comparator to define the order of entities.voidsetListFactory(ListFactory listFactory) Currently only used for non-persisted entities (id == 0).voidsetRemoveFromTargetBox(boolean removeFromTargetBox) On put, this also deletes removed entities from the target Box.intsize()voidsortById()Sorts the list by the "natural" ObjectBox order for to-many list (by entity ID).subList(int start, int end) The returned sub list does not do any change tracking.Object[]toArray()<T> T[]toArray(T[] array) Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
equals, hashCode, replaceAll, sort, spliterator
-
Constructor Details
-
ToMany
-
-
Method Details
-
setListFactory
Currently only used for non-persisted entities (id == 0). -
setComparator
Set an comparator to define the order of entities. -
setRemoveFromTargetBox
@Experimental public void setRemoveFromTargetBox(boolean removeFromTargetBox) On put, this also deletes removed entities from the target Box. Note: removed target entities won't cascade the delete. -
getListFactory
-
add
Adds the given entity to the list and tracks the addition so it can be later applied to the database (e.g. viaBox.put(Object)of the entity owning the ToMany, or viaapplyChangesToDb()). Note that the given entity will remain unchanged at this point (e.g. to-ones are not updated). -
add
Seeadd(Object)for general comments. -
addAll
Seeadd(Object)for general comments. -
addAll
Seeadd(Object)for general comments. -
clear
public void clear() -
contains
-
containsAll
- Specified by:
containsAllin interfaceCollection<TARGET>- Specified by:
containsAllin interfaceList<TARGET>
-
get
-
indexOf
-
isEmpty
public boolean isEmpty() -
iterator
-
lastIndexOf
- Specified by:
lastIndexOfin interfaceList<TARGET>
-
listIterator
- Specified by:
listIteratorin interfaceList<TARGET>
-
listIterator
The returned iterator does not track any potential calls toIterator.remove(). Thus these removes will NOT be synced to the target Box.- Specified by:
listIteratorin interfaceList<TARGET>
-
remove
-
remove
-
removeById
Removes an object by its entity ID. -
removeAll
-
retainAll
-
set
-
size
public int size() -
subList
The returned sub list does not do any change tracking. Thus any modifications to the sublist won't be synced to the target Box. -
toArray
-
toArray
@Nonnull public <T> T[] toArray(T[] array) -
reset
public void reset()Resets the already loaded entities so they will be re-loaded on their next access. This allows to sync with non-tracked changes (outside of this ToMany object). -
isResolved
public boolean isResolved() -
getAddCount
public int getAddCount() -
getRemoveCount
public int getRemoveCount() -
sortById
public void sortById()Sorts the list by the "natural" ObjectBox order for to-many list (by entity ID). This will be the order when you get the entities fresh (e.g. initially or after callingreset()). Note that non persisted entities (ID is zero) will be put to the end as they are still to get an ID. -
applyChangesToDb
public void applyChangesToDb()Applies (persists) tracked changes (added and removed entities) to the target box and/or updates standalone relations. Note that this is done automatically when you put the source entity of this to-many relation. However, if only this to-many relation has changed, it is more efficient to call this method.- Throws:
IllegalStateException- If the source entity of this to-many relation was not previously persisted
-
hasA
Returns true if at least one of the entities matches the given filter.For use with
QueryBuilder.filter(QueryFilter)inside aQueryFilterto check to-many relation entities. -
hasAll
Returns true if all of the entities match the given filter. Returns false if the list is empty.For use with
QueryBuilder.filter(QueryFilter)inside aQueryFilterto check to-many relation entities. -
getById
Gets an object by its entity ID. -
indexOfId
@Beta public int indexOfId(long id) Gets the index of the object with the given entity ID. -
hasPendingDbChanges
public boolean hasPendingDbChanges()Returns true if there are pending changes for the DB. Changes will be automatically persisted once the owning entity is put, or an explicit call toapplyChangesToDb()is made. -
internalCheckApplyToDbRequired
@Internal public boolean internalCheckApplyToDbRequired()For internal use only; do not use in your app. Called after relation source entity is put (so we have its ID). Prepares data forinternalApplyToDb(Cursor, Cursor) -
internalApplyToDb
@Internal public void internalApplyToDb(io.objectbox.Cursor<?> sourceCursor, io.objectbox.Cursor<TARGET> targetCursor) For internal use only; do not use in your app. Convention:internalCheckApplyToDbRequired()must be called before this call as it prepares .
-