Task
objects where task.complete == false
. The second line connects an observer to the query. This is what happens:Task
objects in the future, the query will be executed againio.objectbox.reactive.DataObserver
interface:onData()
is called asynchronously and decoupled from the thread causing the data change (like the thread that committed a transaction).BoxStore
allows a DataObserver
to subscribe to object types. Let’s say we have a to-do list app where Task
objects get added. To get notified when Task
objects are added in another place in our app we can do the following: onData()
is not called with anything useful as data. If you need more than being notified, like to get a list of Task
objects following the above example, read on to learn how to observe queries.subscribe()
which takes no arguments. It subscribes the observer to receive changes for all available object classes.observer()
, it returns a subscription object implementing the io.objectbox.reactive.DataSubscription
interface:DataSubscription
for as long as results should be received, otherwise it can be garbage collected at any point. Also call cancel()
on it once the observer should not be notified anymore, e.g. when leaving the current screen:DataSubscriptionList
instance instead to keep track of multiple DataSubscription
objects. Pass the list to the query.subscribe(subList)
overload. A basic example goes like this:onCreate()/onStart()/onResume()
lifecycle methods and cancel it in its counterpart onDestroy()/onStop()/onPause()
.box.put()
or remove()
individually, an implicit transaction is started and committed. For example, this code fragment would trigger data observers on User.class
twice:runInTx()
or callInTx()
methods in the BoxStore class. For our simple example, we can simply use an overload of put()
accepting multiple objects:DataObserver
notification.on()
call is all that is needed to tell where we want our observer to be called. AndroidScheduler.mainThread()
is a built-in scheduler implementation. Alternatively, you can create an AndroidScheduler
using a custom Looper
, or build a fully custom scheduler by implementing the io.objectbox.reactive.Scheduler
interface.Class
object and returns a Long
number. Thus the DataObserver
receives the object count as a Long
parameter in onData()
.io.objectbox.reactive.Transformer
interface for clarification of what the transform()
method expects as a parameter:DataObserver
might throw a RuntimeException
. In both cases, you can provide an ErrorObserver
to be notified about an exception that occurred. The io.objectbox.reactive.ErrorObserver
is straight-forward:ErrorObserver
, simply call the onError()
method after subscribe()
.DataObserver
gets both of the following by default:single()
and onlyChanges()
are for (call them after subscribe()
). Single subscriptions are special in the way that they are cancelled automatically once the observer is notified. You can still cancel them manually to ensure no call to the observer is made at a certain point.weak()
after subscribe()
.DataTransformer
runs on a background thread (exclusive for this task)DataObserver
and ErrorObserver
run on a background thread unless a scheduler is specified via the on()
method.RxQuery
and RxBoxStore
. Both offer static methods to subscribe using RxJava means.RxBoxStore
to create an Observable
. RxQuery
allows to subscribe to query objects using: