// objectbox:entity
before the type as an annotation for the code generator.init()
, and you're good to go:Id
with 0 for all entities. This tells ObjectBox your object isn't persisted, yet. When you persist objects, the ID will be changed to the actual value. And once an entity has a non-zero ID, persisting changes will update the existing record instead of creating a new one.ObjectBox.Store
class. The Store
behaves much like a database connection: you keep the instance around to maintain an open connection to the data in a folder on disk. Usually for the lifetime of your app..applicationSupportDirectory
, .documentsDirectory
or .cachesDirectory
:Box
interface. For each object class, there is a matching Box
instance.ObjectBox.Box<T>
instance for its class from yourStore
. Boxes are lightweight and can be discarded, but then you have to pass the Store
around. You will almost always prefer to pass long-lived Box
instances around instead, for example in segues between UIViewControllers
.put
to insert or update objects. When put succeeds, an ID will be assigned to the entity. There are put
variants that support writing multiple objects at once, which is more efficient than writing each with its own call to put
.get
. To get all objects of a type, use all
.removeAll
to delete all objects and empty the box.putAndReturnID()
or putAndReturnIDs()
to get the ID a struct was written as:put(struct:)
, which will automatically create a new copy of your struct with the ID filled out:exampleEntity
, you must use newEntity
from then on. If you called put(struct:)
or put()
on exampleEntity again, ObjectBox would not know that this object has already been saved, and would write a second copy of it to the database.put()
, just like above:init()
method on your struct that accepts all its persisted properties. In the above example, the default initializer provided for the struct by Swift does just fine, but if you have properties that are not persisted, or you are defining your own initializer, you may also have to provide that initializer.put
runs an implicit transaction.put
variant that takes an array of entities (like put([person1, person2])
) whenever possible to increase performance.Store
exposes methods like runInTransaction
for this purpose.