ToOne
class, a smart proxy to the target object. It gets and caches the target object transparently. Order
class to have a to-one relation to the Customer
like this:ToOne
will put()
it. If it already exists, the ToOne
will only create the relation (but not put()
it).nil
and 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:ToOne
is a Lazy Relation ProxyToOne.target
, it will not be read into main memory.ToMany
. Like the ToOne class, the ToMany
class helps you to keep track of changes and to apply them to the database.ToMany
object. So subsequent calls to any method, like the count
of 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 reset()
on the ToMany
.ToOne
relation 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 ToOne
will 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 ToMany
from the Customer referencing its two Order objects.Customer
to Order
:// objectbox: backlink = "name"
annotation (where name is the name of the ToOne property that makes up the other end of the relation).ToMany
ToMany
conforms to Swift's RandomAccessCollection
protocol. 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:ToMany
relation is also a RangeReplaceableCollection
. That means you can use append(_:)
etc. on it just like on an Array
to modify it. We've also added a replace(_:)
method as a convenience to replace all entities referenced by the relation.applyToDb()
on the ToMany
to actually cause them to be written to the database. Note that ToMany
applies change tracking and thus only writes updated relations to the database.ToMany
will simply set the ToOne
relation in the removed entities to nil
, and will make added entities' ToOne
point at the object containing the ToMany
backlink. Note that, starting from version 1.4, you can add new (not yet persisted) objects, which applyToDb()
will put automatically :ToMany
backlink modifies the ToOne
of the referenced entities (in this example, newOrder
and 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 replace()
:ToMany
class. Assuming a students and teachers example, this is how a simple Student
class that has a to-many relation to Teacher
entities can look like:// objectbox: backlink
annotation:teachers
, and that students
is just a reverse-lookup of this relation. In any other respect, a many-to-many backlink can be used just like its forward counterpart.