Relations
-
Object property type to indicate a to-one relation to another object.
Initialize with
nilin your type declarations.- You can set the value to
nilto remove the relation. You can also settargetortargetIdtonil. - You can set
targetto an object to set the relation. CallBox.put(_:)to persist the changes. - You can set
targetIdto an object’s ID to set the relation. CallBox.put(_:)to persist the changes.
Example
class Person: Entity { var spouse: ToOne<Person> = nil } let personBox: Box<Person> = store.box(for: Person.self) let amanda: Person = ... let neil: Person = ... amanda.spouse.target = neil try personBox.put(amanda)Remove a relation
See moreamanda.spouse.target = nil // ... or ... amanda.spouse.targetId = nil // ... are equivalent to: amanda.spouse = nil // ... whis is a short version of: amanda.spouse = ToOne<Person>(target: nil)Declaration
Swift
public final class ToOne<T: EntityInspectable & __EntityRelatable>: ExpressibleByNilLiteral where T == T.EntityBindingType.EntityTypeextension ToOne: CustomStringConvertibleextension ToOne: CustomDebugStringConvertible - You can set the value to
-
Declaration of a to-many relationship to objects of a certain type.
Initialize with
nilin your type declarations. The code generator will set different values.Example:
class Customer: Entity { var id: EntityId<Customer> = 0 /// Annotation with ReferencedType's property name is required; Order is "ReferencedType", /// Customer is "OwningType" // objectbox: backlink = "customer" var orders: ToMany<Order> = nil // ... } class Order: Entity { var id: EntityId<Order> = 0 var customer: ToOne<Customer> = nil // ... }Removing relations
A ToMany can be modified just like any other RangeReplaceableCollection. Once you have changed your relation as needed, call
applyToDb()on it to actually write the changes to disk.See moreNote
You can also use a ToMany to create backlinks fromToOnerelations. Use the// objectbox: backlink = "propertyName"annotation to tell the code generator which property ofToMany.ReferencedTypeshould be used to determine the backlinks.Declaration
Swift
public final class ToMany<S: EntityInspectable & __EntityRelatable>: ExpressibleByNilLiteral where S == S.EntityBindingType.EntityTypeextension ToMany: RandomAccessCollectionextension ToMany: RangeReplaceableCollectionextension ToMany: CustomStringConvertibleextension ToMany: CustomDebugStringConvertible
View on GitHub
Install in Dash
Relations Reference