ToOneclass, a smart proxy to the target object. It gets and caches the target object transparently.
Orderclass to have a to-one relation to the
niland persist the changed object(s) via
Box.put()to remove a relation permanently. Which one you reset doesn't matter, though. You have these options:
ToOneis a Lazy Relation Proxy
ToOne.target, it will not be read into main memory.
ToMany. Like the ToOne class, the
ToManyclass helps you to keep track of changes and to apply them to the database.
ToManyobject. So subsequent calls to any method, like the
ToMany, do not query the database, even if the relation was changed elsewhere. To avoid the cache and trigger a fresh reload from the database, call
ToOnerelation that you have, you can define a backlink. Backlinks are using the same relation information, but in the reverse direction. Thus, a backlink of a
ToOnewill result in a list of potentially multiple objects: all entities pointing to the same entity. Example: Two Order objects point to the same Customer using a
ToOne. The backlink is a
ToManyfrom the Customer referencing its two Order objects.
// objectbox: backlink = "name"annotation (where name is the name of the ToOne property that makes up the other end of the relation).
ToManyconforms to Swift's
RandomAccessCollectionprotocol. Thus, you can use it just like an array or similar collections and pass it around. And of course, you can create an Array if need be:
ToManyrelation is also a
RangeReplaceableCollection. That means you can use
append(_:)etc. on it just like on an
Arrayto modify it. We've also added a
replace(_:)method as a convenience to replace all entities referenced by the relation.
ToManyto actually cause them to be written to the database. Note that
ToManyapplies change tracking and thus only writes updated relations to the database.
ToManywill simply set the
ToOnerelation in the removed entities to
nil, and will make added entities'
ToOnepoint at the object containing the
ToManybacklink. Note that, starting from version 1.4, you can add new (not yet persisted) objects, which
applyToDb()will put automatically :
ToManybacklink modifies the
ToOneof the referenced entities (in this example,
oldOrder) and will
put()those objects to actually write out the changed relation. To remove all references to an entity, you may pass an empty array to
ToManyclass. Assuming a students and teachers example, this is how a simple
Studentclass that has a to-many relation to
Teacherentities can look like:
// objectbox: backlinkannotation:
teachers, and that
studentsis just a reverse-lookup of this relation. In any other respect, a many-to-many backlink can be used just like its forward counterpart.