Decisions, Decisions

To determine what Rx type is appropriate for your case, you need to make two decisions:

  1. Do you just want the data once, or do you want to be notified about updates over time?
  2. How many objects might you get in your result set from your query? 0? 1? N?

One-Time or Ongoing?

In some cases, you are just performing a query to get some data out of the database, and that’s it.

However, Room offers a powerful feature whereby it will re-execute your query and deliver you fresh results, if somewhere else Room sees that you manipulated the contents of the table(s) that your query uses. The result is a bit like a ContentProvider and ContentObserver, in that you get fresh results delivered “automatically”. However, to make this work, you will need to use particular Rx types that support ongoing events, not just one-time events.

Zero, One, or N?

Sometimes, you are fairly certain how many rows will be returned by your query, and therefore how many entities or other POJOs will be returned by a DAO method. For example, if you are querying by primary key or some other unique column, you know that you will only ever get back 0 or 1 objects, as there cannot be more than one with the unique value. Similarly, if you are using simple aggregate functions (e.g., SELECT COUNT(*) FROM table WHERE criteria...), you know that you will get exactly one row back.

In other cases, you may have no idea how many rows you will get. Querying for all rows in a table might return 0, 1, or N rows, depending on the state of the table at the time. In these cases, you may want to have reactive types that return a List of objects, rather than just an individual object.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.