Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added method to put entities in a box using a single transaction and … #339

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions objectbox-java/src/main/java/io/objectbox/Box.java
Original file line number Diff line number Diff line change
@@ -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.
*/