API Fact: The box.get()
Method
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])