@ThreadSafe
public class BoxStore
extends java.lang.Object
implements java.io.Closeable
Box
es to get and put Objects of a specific type
(see boxFor(Class)
).Modifier and Type | Method and Description |
---|---|
io.objectbox.Transaction |
beginReadTx()
Internal, low level method: use
runInReadTx(Runnable) instead. |
io.objectbox.Transaction |
beginTx()
Internal, low level method: use
runInTx(Runnable) instead. |
<T> Box<T> |
boxFor(java.lang.Class<T> entityClass)
Returns a Box for the given type.
|
<T> T |
callInReadTx(java.util.concurrent.Callable<T> callable)
Calls the given callable inside a read(-only) transaction.
|
<T> T |
callInReadTxWithRetry(java.util.concurrent.Callable<T> callable,
int attempts,
int initialBackOffInMs,
boolean logAndHeal)
Calls
callInReadTx(Callable) and retries in case a DbException is thrown. |
<R> R |
callInTx(java.util.concurrent.Callable<R> callable)
Like
runInTx(Runnable) , but allows returning a value and throwing an exception. |
<R> void |
callInTxAsync(java.util.concurrent.Callable<R> callable,
TxCallback<R> callback)
Runs the given Runnable as a transaction in a separate thread.
|
<R> R |
callInTxNoException(java.util.concurrent.Callable<R> callable)
Like
callInTx(Callable) , but throws no Exception. |
int |
cleanStaleReadTransactions() |
static boolean |
clearDefaultStore()
Clears the convenience instance.
|
void |
close()
Closes the BoxStore and frees associated resources.
|
void |
closeThreadResources()
Call this method from a thread that is about to be shutdown or likely not to use ObjectBox anymore:
it frees any cached resources tied to the calling thread (e.g. readers).
|
boolean |
deleteAllFiles()
Danger zone!
|
static boolean |
deleteAllFiles(java.io.File objectStoreDirectory)
Danger zone!
|
static boolean |
deleteAllFiles(java.io.File baseDirectoryOrNull,
java.lang.String customDbNameOrNull)
Danger zone!
|
static boolean |
deleteAllFiles(java.lang.Object androidContext,
java.lang.String customDbNameOrNull)
Danger zone!
|
java.lang.String |
diagnose()
Gives info that can be useful for debugging.
|
protected void |
finalize() |
java.util.Collection<java.lang.Class> |
getAllEntityClasses() |
static BoxStore |
getDefault()
Convenience singleton instance which gets set up using
BoxStoreBuilder.buildDefault() . |
int |
getEntityTypeIdOrThrow(java.lang.Class entityClass) |
int |
getObjectBrowserPort() |
static java.lang.String |
getVersion() |
static java.lang.String |
getVersionNative() |
TxCallback |
internalFailedReadTxAttemptCallback() |
int |
internalQueryAttempts() |
java.util.concurrent.Future |
internalScheduleThread(java.lang.Runnable runnable) |
java.util.concurrent.ExecutorService |
internalThreadPool() |
boolean |
isClosed() |
boolean |
isDebugRelations() |
static boolean |
isObjectBrowserAvailable() |
void |
runInReadTx(java.lang.Runnable runnable)
Runs the given runnable inside a read(-only) transaction.
|
void |
runInTx(java.lang.Runnable runnable)
Runs the given runnable inside a transaction.
|
void |
runInTxAsync(java.lang.Runnable runnable,
TxCallback<java.lang.Void> callback)
Runs the given Runnable as a transaction in a separate thread.
|
void |
setDbExceptionListener(DbExceptionListener dbExceptionListener)
The given listener will be called when an exception is thrown.
|
java.lang.String |
startObjectBrowser() |
java.lang.String |
startObjectBrowser(int port) |
SubscriptionBuilder<java.lang.Class> |
subscribe()
A
DataObserver can be subscribed to data changes using the returned builder. |
<T> SubscriptionBuilder<java.lang.Class<T>> |
subscribe(java.lang.Class<T> forClass)
Like
subscribe() , but wires the supplied @DataObserver only to the given
object class for notifications. |
static void |
testUnalignedMemoryAccess()
Diagnostics: If this method crashes on a device, please send us the logcat output.
|
void |
unregisterTransaction(io.objectbox.Transaction transaction) |
public static BoxStore getDefault()
BoxStoreBuilder.buildDefault()
.
Note: for better testability, you can usually avoid singletons by storing
a BoxStore
instance in some application scope object and pass it along.
public static boolean clearDefaultStore()
Note: This is usually not required (for testability, please see the comment of getDefault()
).
public static java.lang.String getVersionNative()
public static void testUnalignedMemoryAccess()
public static boolean isObjectBrowserAvailable()
public static java.lang.String getVersion()
protected void finalize() throws java.lang.Throwable
finalize
in class java.lang.Object
java.lang.Throwable
@Internal public int getEntityTypeIdOrThrow(java.lang.Class entityClass)
public java.util.Collection<java.lang.Class> getAllEntityClasses()
@Internal public io.objectbox.Transaction beginTx()
runInTx(Runnable)
instead.@Internal public io.objectbox.Transaction beginReadTx()
runInReadTx(Runnable)
instead.
Begins a transaction for read access only. Note: there may be only one read transaction per thread.public boolean isClosed()
public void close()
WARNING: This is a somewhat delicate thing to do if you have threads running that may potentially still use the BoxStore. This results in undefined behavior, including the possibility of crashing.
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
public boolean deleteAllFiles()
close()
before and read the docs of that method carefully!
A safer alternative: use the static deleteAllFiles(File)
method before opening the BoxStore.
public static boolean deleteAllFiles(java.io.File objectStoreDirectory)
If you did not use a custom name with BoxStoreBuilder, you can pass "new File(BoxStoreBuilder.DEFAULT_NAME
)".
objectStoreDirectory
- directory to be deleted; this is the value you previously provided to BoxStoreBuilder.directory(File)
public static boolean deleteAllFiles(java.lang.Object androidContext, @Nullable java.lang.String customDbNameOrNull)
If you did not use a custom name with BoxStoreBuilder, you can pass "new File(BoxStoreBuilder.DEFAULT_NAME
)".
androidContext
- provide an Android Context like Application or ServicecustomDbNameOrNull
- use null for default name, or the name you previously provided to BoxStoreBuilder.name(String)
.public static boolean deleteAllFiles(@Nullable java.io.File baseDirectoryOrNull, @Nullable java.lang.String customDbNameOrNull)
If you did not use a custom name with BoxStoreBuilder, you can pass "new File(BoxStoreBuilder.DEFAULT_NAME
)".
baseDirectoryOrNull
- use null for no base dir, or the value you previously provided to BoxStoreBuilder.baseDirectory(File)
customDbNameOrNull
- use null for default name, or the name you previously provided to BoxStoreBuilder.name(String)
.@Internal public void unregisterTransaction(io.objectbox.Transaction transaction)
public <T> Box<T> boxFor(java.lang.Class<T> entityClass)
public void runInTx(java.lang.Runnable runnable)
Efficiency notes: it is advised to run multiple puts in a transaction because each commit requires an expensive disk synchronization.
public void runInReadTx(java.lang.Runnable runnable)
@Experimental public <T> T callInReadTxWithRetry(java.util.concurrent.Callable<T> callable, int attempts, int initialBackOffInMs, boolean logAndHeal)
callInReadTx(Callable)
and retries in case a DbException is thrown.
If the given amount of attempts is reached, the last DbException will be thrown.
Experimental: API might change.public <T> T callInReadTx(java.util.concurrent.Callable<T> callable)
public <R> R callInTx(java.util.concurrent.Callable<R> callable) throws java.lang.Exception
runInTx(Runnable)
, but allows returning a value and throwing an exception.java.lang.Exception
public <R> R callInTxNoException(java.util.concurrent.Callable<R> callable)
callInTx(Callable)
, but throws no Exception.
Any Exception thrown in the Callable is wrapped in a RuntimeException.public void runInTxAsync(java.lang.Runnable runnable, @Nullable TxCallback<java.lang.Void> callback)
See also runInTx(Runnable)
.
public <R> void callInTxAsync(java.util.concurrent.Callable<R> callable, @Nullable TxCallback<R> callback)
* See also callInTx(Callable)
.
public java.lang.String diagnose()
public int cleanStaleReadTransactions()
public void closeThreadResources()
Box.closeThreadResources()
for all initiated boxes (boxFor(Class)
).public SubscriptionBuilder<java.lang.Class> subscribe()
DataObserver
can be subscribed to data changes using the returned builder.
The observer is supplied via SubscriptionBuilder.observer(DataObserver)
and will be notified once a
transaction is committed and will receive changes to any object class.
Threading notes: All observers are notified from one separate thread (pooled). Observers are not notified in parallel. The notification order is the same as the subscription order, although this may not always be guaranteed in the future.
Note that failed or aborted transaction do not trigger observers.
@Experimental @Nullable public java.lang.String startObjectBrowser()
@Experimental @Nullable public java.lang.String startObjectBrowser(int port)
@Experimental public int getObjectBrowserPort()
public void setDbExceptionListener(DbExceptionListener dbExceptionListener)
public <T> SubscriptionBuilder<java.lang.Class<T>> subscribe(java.lang.Class<T> forClass)
subscribe()
, but wires the supplied @DataObserver
only to the given
object class for notifications.@Internal public java.util.concurrent.Future internalScheduleThread(java.lang.Runnable runnable)
@Internal public java.util.concurrent.ExecutorService internalThreadPool()
@Internal public boolean isDebugRelations()
@Internal public int internalQueryAttempts()
@Internal public TxCallback internalFailedReadTxAttemptCallback()
Available under the Apache License, Version 2.0 - Copyright © 2017 ObjectBox Ltd. All Rights Reserved.