https://kotlinlang.org logo
#android
Title
# android
r

Robert Menke

04/22/2018, 11:29 PM
For android I’d stick with the Rooms ORM. It’s excellent.
g

gildor

04/23/2018, 1:50 AM
Room is not ORM, there is no R - relations. You can use relations between tables, but only explicitly
r

Robert Menke

04/23/2018, 3:11 AM
Fair point. Is there a database abstraction for android that you recommend in place of Rooms?
g

gildor

04/23/2018, 3:30 AM
I would recommend Room in place of Room 😬
I completely agree with Room authors that implicit relations on mobile is bad choice. It can work for backend, but this make work with DB on mobile where you should worry about lifecycle and UI thread not predictable Even when I use some ORM on Android I avoid using relations, use them just like sqlite helper
So my opinion that Room or similar approach (that already used by some other libraries, for example sqldelight) is the best knows solution for db helpers on mobile
Also, Room and sqldelight expose SQL but validate them, so you get power for SQL and type safety
r

Robert Menke

04/23/2018, 5:36 PM
What do you mean by implicit relationships though? Every library I’ve used that calls itself an ORM only allows for explicit relationships.
g

gildor

04/24/2018, 1:13 AM
You right, meant implicit request to related database, imagine some DB object User that has relation to Address, each of those objects in a separate table, so ORM allows you to connect them explicitly, you just annotate related fields (or use some other way to connect 2 tables), but then when you requested User and want to get Address, ORM will request this Address for you. So there are 2 ways to work with such relations: eager (request all the data from all the tables) or lazy (request data when property/method with related data called for the first time). Both those approaches has own problems Everything become even more complicated when you allow to change data using Active Objects patter or even just using setter, there are a lot of problems (threading, IO operations, sync/async access) some of them has workarounds, but ORM become more and more complicated (check for example Core Data from iOS)
r

Robert Menke

04/24/2018, 3:28 AM
@gildor Ok that makes more sense 🙂. I would still consider Room to be an ORM unless I’m missing some subtleties of that definition. It maps database queries to objects and allows you to define relationships. The reduced complexity is actually great in my opinion.
g

gildor

04/24/2018, 3:31 AM
It allows you to map relationships but only in very explicit way: join two entities and map them to new model (no nested relations of two models, it’s just mapping of
SELECT JOIN
to some data object). So this is O and M - object mapping, no R Also you can check official docs authdors do not claim that Room is ORM
But maybe I was not enough clear. Room is not ORM and it’s the best thing about Room. It reduces boilerplate, provide mapping, exposes raw SQL and completely type safe So Room is the best approach to work with sqlite on Andrroid imo and main thing why it’s the best is that Room is not ORM
r

Robert Menke

04/24/2018, 12:41 PM
Agreed! Thanks for the reply.
n

nil2l

07/18/2018, 8:34 AM
@gildor
Everything become even more complicated when you allow to change data using Active Objects patter or even just using setter, there are a lot of problems (threading, IO operations, sync/async access) some of them has workarounds, but ORM become more and more complicated (check for example Core Data from iOS)
Could you explain, what exactly become a problem when ORM joins tables implicit? And what is about Code Data? Is it like Room or ORM? Thx
g

gildor

07/18/2018, 8:35 AM
Do you mean Core Data from Cocoa? Core Data is classic ORM and IMO it’s terrible and has all the problems of ORMs on mobile
n

nil2l

07/18/2018, 8:36 AM
Yes, by Apple.
g

gildor

07/18/2018, 8:37 AM
Could you explain
Because you do not control threading, you just don’t know if this getters/setter is syncronous or asyncronous and Core Data has a lot of hacks how to update data across threads using caches and merge of those caches
Main problem of ORM with those implicit getters/setters - some operations are asynchronous, some not and pure getter and setter are synchronous by nature, do not provide any async abstraction (only coroutines can do that). Usual solution: just block thread if you want to read or write something, but this is terrible choice for UI, you don’t want to block UI thread, you also should respect lifecycle
n

nil2l

07/18/2018, 8:42 AM
Hm.. But why not just make callback, start new thread with all these sync job and then get result though callback?
g

gildor

07/18/2018, 8:44 AM
So, you suggest to wrap all work with database to a background thread. Great choice, but you cannot anymore access ORM data object getters and setters
You cannot just load some class
User
and access property
address
in ORM way. When address will be loaded dynamically on access
But if you remove this
R
-
Relations
and request user together with address as data object (without save() method) from some async API you will get some other abstraction, not ORM, but what Room is: DB helper with object mapping
n

nil2l

07/18/2018, 8:50 AM
Oh… sorry. Maybe it’s not clear for me as I don’t have experience with iOS. Seems like objects there are accessable only from the main thread. Basicly I use Kotlin and coroutines for async taks in Android. Just want to understand what’s wrong here with an implicit joining (relationship).
g

gildor

07/18/2018, 8:50 AM
another problem of Core Data and any ORM with Active Records pattern. You all the time leak your setters or
save()
methods for the record, you cannot make you data immutable and you have a lot of problem with it (you passed data to another thread and this thread changed something, welcome to hell now). To avoid that you need an interface only with getters and without ORM methods but again, you need some wrapper that hides your ORM data objects from consumer. And Room handles all this perfectly. No extension of basic class, no “save()” method on data models. But you just have a separate DAO method where you pass updated data
Seems like objects there are accessable only from the main thread
It’s not exactly true, you can manage it in multiple threads, but it’s pretty tricky and error prone thing
what’s wrong here with an implicit joining
Even with coroutines you can have problems there. What if you want to display list of users with their address in RecyclerView? Yes, coroutines provide you async wrapper and it’s fine, but to use it for such use cases you need to prefetch all the data before usage in list. Or you need some loaders on each piece of data on your view on RecyclerView, also possible of course
But why not just fetch data that you need using one request. One more good thing about Room, that you can fetch only required fields and have special data models for particular case instead of “fetch everything” or “fetch root model and fetch submodels lazily”
n

nil2l

07/18/2018, 8:55 AM
Look like it gives more control.
g

gildor

07/18/2018, 8:55 AM
It’s still holly war topic, you can find a lot of successful usages of ORMs for many apps. I do not want to say ORMs must die or something like that. I just think that there are better options for UI apps and approach of Room or SQLBrite + SQLDelight looks much better for UI case than ORM
n

nil2l

07/18/2018, 8:58 AM
Ok, understand. Thanks for detailed explanations. 👍
g

gildor

07/18/2018, 8:59 AM
There are many other things about DB, for example how do you write your requests. Room and SQLBrite have imo best way to do that using pure SQL with compile time validation, rather than building SQL strings using some builders (as most ORMs do) Really good talk that covers this problem https://slideslive.com/38898753/embracing-sql-without-abstraction