7

Is it a good idea to use an ORM (Object Relational Mapper) like:

  • ORMLite
  • ActiveAndroid

for Android apps.

The abstraction layer this technique adds has itself calculation and memory overhead. Since, those resources and the battery lifetime are mostly very limited I would think 'no', but on the other hand it makes the code much cleaner, because one almost never has to write raw queries.

  • What are the pros and cons?
  • What is recommended?
  • If it is recommended, which one should I use?
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121

2 Answers2

8

As of May 18, 2017, Google introduced Room at I/O '17 as replacement for raw SQLite queries. So, at least this ORM is now officially recommended.

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
  • Room supports only basic SQLite features. For example, it does not support `upsert`: https://sqlite.org/lang_upsert.html – James Bond Dec 22 '21 at 21:08
1

OrmLite use SQLite database underneath. It just generates database schema using annotation processor. It is a good idea if your project allows using third- party opensource libraries. However, you should incapsulate all implementation - dependent features - 'Dao' pattern. By doing so, you'll be able to switch between database implementations. BE careful with Realm though. It is a very powerful database, but if you use it incorrectly, it can ruin your architecture. Always specify Dao interface before implementing database itself.

Alex Shutov
  • 3,217
  • 2
  • 13
  • 11
  • so, the annotation processor runs at compile-time and gives no runtime performance decrease? – Willi Mentzel May 03 '17 at 08:07
  • It creates schema at compile time and then use reflections. It gives some decrease, but those a very small compared to database operation time – Alex Shutov May 03 '17 at 08:08
  • I see, but why is reflection still necessary? All the code could have been generated from the annotations? So, if I aim for performance, should I avoid ORM? – Willi Mentzel May 03 '17 at 08:10
  • ok, I see... so, is it actually recommended by Google to use an ORM? Which one should I use (edit of my question). – Willi Mentzel May 03 '17 at 08:14
  • Usually I use OrmLite or (sometimes) Realm, because those are easy to use. Realm is a native noSql database, I use it when app should store some readings on disc, in separate files. – Alex Shutov May 03 '17 at 08:19