From 63eb2ea2bf6327ba327caae5d93a825c7e5be754 Mon Sep 17 00:00:00 2001 From: Gennadiy Kuchergin <gena_kuchegin@mail.ru> Date: Thu, 18 Jan 2018 09:14:37 +0300 Subject: [PATCH] added method to put entities in a box using a single transaction and return list of saved entities ids --- .../src/main/java/io/objectbox/Box.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/objectbox-java/src/main/java/io/objectbox/Box.java b/objectbox-java/src/main/java/io/objectbox/Box.java index 6ee52587..b9aaa6b7 100644 --- a/objectbox-java/src/main/java/io/objectbox/Box.java +++ b/objectbox-java/src/main/java/io/objectbox/Box.java @@ -401,6 +401,34 @@ public void put(@Nullable Collection<T> entities) { } } + /** + * Puts the given entities in a box using a single transaction. + * + * @param entities It is fine to pass null or an empty collection: + * this case is handled efficiently without overhead. + * + * @return list of saved entities ids + */ + public Collection<Long> putAndGetIds(@Nullable Collection<T> entities) { + if (entities == null || entities.isEmpty()) { + return Collections.emptyList(); + } + + Collection<Long> ids = new ArrayList<>(); + Cursor<T> cursor = getWriter(); + try { + for (T entity : entities) { + ids.add(cursor.put(entity)); + } + commitWriter(cursor); + } catch (Exception e) { + ids = Collections.emptyList(); + } finally { + releaseWriter(cursor); + } + return ids; + } + /** * Removes (deletes) the Object by its ID. */