androidkotlinandroid-room

Room: how to check whether row exists


In my repository class, I want to fetch data only if none exists in a Room table. How to check whether any row exists in a table?


Solution

  • As ADM suggested, you might get lucky using COUNT() to count the actual rows in a table.

    However, I would recommend just fetching the data anyway - if none exists Room will simply return an empty list, and this should not be less efficient than asking for the row count (if it's 0 anyway).

    As a plus you will have less code to write to get the functionality you want! :-)