**Room **is an ORMObject Relational Mapping library, which means that Room will map our database objects to Java objects.

Room provides an abstraction layer over SQLite to allow fluent database access while harnessing the full power of **SQLite **and now it is considered as a better approach for data persistence than SQLiteDatabase.

Why use Room?

  • Compile-time verification of SQL queries, @Query, and @**Entity **objects, which prevents runtime app crashing
  • Easily integrated with other Architecture components (like LiveData), in other words, it is built to work with **LiveData **and **RxJava **for data observation while **SQLite **is not

SQLite’s disadvantages

  • no compile-time verification of raw SQL queries, so it is impossible to catch wrong column name in a query problem during a compile-time
  • with schema’s changes, SQL queries updates are unavoidable

Room vs Anko

Anko SQLite provides a nice API to manage your data persistence layer but you still have to do the heavy lifting by yourself. While Room is more like a framework. Generates databases from annotated classes for you provides observable queries and has really nice testing support. Also works well with Android Architecture Components. (stackoverflow)

Room vs Realm

Realm

A relatively fast and convenient library, all links are simply implemented, which is related to the object orientation of the database. Excellent documentation. Is, perhaps, one of the best options for storing data on a mobile device at the moment, the minus can only be an increase in the size of the apk-file by 2.5 MB.

Room

An interesting solution presented on Google I / O 2017 as optimal for working with the database on Android OS. Despite the fact that it is necessary to use explicit sql-requests, the library turned out to be quite convenient and I liked it personally. On performance is in the lead, so I would advise you to choose this particular library. A big advantage of this is based on the build-in SQLite database. Since this solution, submitted by Google, it will quickly become popular, and, therefore, there will be no problems with finding solutions to problems that occur along with it.

Realm uses more RAM and increases the apk size, build time. So I prefer Room.- Alexey Zatsepin (stackoverflow)

A conclusion from the more detailed comparison between performance of difference ORMs from 2017:

Summing up, the most “fast” ORMs were Realm, GreenDAO, ORMLite and Room, but if performance is critical for the project, a “clean” SQLite with a custom cache is still the best choice. On the other parameters, the best are Realm, DBFlow and GreenDAO. Thus, for small projects and projects of medium complexity, I recommend using DBFlow or GreenDAO, if the size of apk is not important — Realm. Realm and Room are suitable for large projects. And only if their capabilities for some reason are not enough, or the ORM-approach is not for you, to return to using the built-in API for SQLite.

#android #android-apps #android-room #room #java

How to Use Room | Android (JetPack) | PiLove notes
4.85 GEEK