How do I retrieve an object in ObjectBox?
Answer: Use box.get()
- Retrieves a single object by its ID.
- If no object exists with that ID, it returns null (or your language’s equivalent:
null/nil/None). - To fetch several objects at once, use the multi‑get variant (e.g.,
getMany(...),get(List<Id>), or the platform’s equivalent).
Purpose
The box.get() method looks up and returns one object by its unique ObjectBox ID. Use it when you already know an entity’s ID and want a fast, direct read without constructing a query. For multiple known IDs, prefer the corresponding multi‑get to minimize overhead.
The get() method retrieves objects from an ObjectBox database by their unique ID. Use it to fetch a single object or multiple objects efficiently.
Single-ID Lookup
For a single ID, get(id) returns the object or null/None if no object with that ID exists.
- Java
- Kotlin
- Swift
- Dart
- Python
Java example
MyObject object = box.get(1);
Kotlin example
val obj: MyObject? = box.get(1)
Swift example
let obj: MyObject? = box.get(1)
Dart example
final object = box.get(1);
Python example
obj = box.get(1)
Multi-ID Lookup
For multiple IDs, use the language-specific bulk read method to avoid repeated calls:
- Java/Kotlin:
get(long[] ids) - Dart:
getMany(ids) - Swift/Python: use the equivalent multi-ID API.
- Java
- Kotlin
- Swift
- Dart
- Python
Java example (multi)
List<MyObject> objects = box.get(new long[]{1, 2, 3});
Kotlin example (multi)
val objs: List<MyObject?> = box.get(longArrayOf(1, 2, 3))
Swift example (multi)
let objects = box.getMany([1, 2, 3])
Dart example (multi)
final objects = box.getMany([1, 2, 3]);
Python example (multi)
objs = box.get_many([1, 2, 3])