https://kotlinlang.org logo
Channels
100daysofcode
100daysofkotlin
100daysofkotlin-2021
advent-of-code
aem
ai
alexa
algeria
algolialibraries
amsterdam
android
android-architecture
android-databinding
android-studio
androidgithubprojects
androidthings
androidx
androidx-xprocessing
anime
anko
announcements
apollo-kotlin
appintro
arabic
argentina
arkenv
arksemdevteam
armenia
arrow
arrow-contributors
arrow-meta
ass
atlanta
atm17
atrium
austin
australia
austria
awesome-kotlin
ballast
bangladesh
barcelona
bayarea
bazel
beepiz-libraries
belgium
benchmarks
berlin
big-data
books
boston
brazil
brikk
budapest
build
build-tools
bulgaria
bydgoszcz
cambodia
canada
carrat
carrat-dev
carrat-feed
chicago
chile
china
chucker
cincinnati-user-group
cli
clikt
cloudfoundry
cn
cobalt
code-coverage
codeforces
codemash-precompiler
codereview
codingame
codingconventions
coimbatore
collaborations
colombia
colorado
communities
competitive-programming
competitivecoding
compiler
compose
compose-android
compose-desktop
compose-hiring
compose-ios
compose-mp
compose-ui-showcase
compose-wear
compose-web
confetti
connect-audit-events
corda
cork
coroutines
couchbase
coursera
croatia
cryptography
cscenter-course-2016
cucumber-bdd
cyprus
czech
dagger
data2viz
databinding
datascience
dckotlin
debugging
decompose
decouple
denmark
deprecated
detekt
detekt-hint
dev-core
dfw
docs-revamped
dokka
domain-driven-design
doodle
dsl
dublin
dutch
eap
eclipse
ecuador
edinburgh
education
effective-kotlin
effectivekotlin
emacs
embedded-kotlin
estatik
event21-community-content
events
exposed
failgood
fb-internal-demo
feed
firebase
flow
fluid-libraries
forkhandles
forum
fosdem
fp-in-kotlin
framework-elide
freenode
french
fritz2
fuchsia
functional
funktionale
gamedev
ge-kotlin
general-advice
georgia
geospatial
german-lang
getting-started
github-workflows-kt
glance
godot-kotlin
google-io
gradle
graphic
graphkool
graphql
graphql-kotlin
graviton-browser
greece
grpc
gsoc
gui
hackathons
hacktoberfest
hamburg
hamkrest
helios
helsinki
hexagon
hibernate
hikari-cp
hire-me
hiring
hongkong
hoplite
http4k
hungary
hyderabad
image-processing
india
indonesia
inkremental
intellij
intellij-plugins
intellij-tricks
internships
introduce-yourself
io
ios
iran
israel
istanbulcoders
italian
jackson-kotlin
jadx
japanese
jasync-sql
java-to-kotlin-refactoring
javadevelopers
javafx
javalin
javascript
jdbi
jhipster-kotlin
jobsworldwide
jpa
jshdq
juul-libraries
jvm-ir-backend-feedback
jxadapter
k2-early-adopters
kaal
kafka
kakao
kalasim
kapt
karachi
karg
karlsruhe
kash_shell
kaskade
kbuild
kdbc
kgen-doc-tools
kgraphql
kinta
klaxon
klock
kloudformation
kmdc
kmm-español
kmongo
knbt
knote
koalaql
koans
kobalt
kobweb
kodein
kodex
kohesive
koin
koin-dev
komapper
kondor-json
kong
kontent
kontributors
korau
korean
korge
korim
korio
korlibs
korte
kotest
kotest-contributors
kotless
kotlick
kotlin-asia
kotlin-beam
kotlin-by-example
kotlin-csv
kotlin-data-storage
kotlin-foundation
kotlin-fuel
kotlin-in-action
kotlin-inject
kotlin-latam
kotlin-logging
kotlin-multiplatform-contest
kotlin-mumbai
kotlin-native
kotlin-pakistan
kotlin-plugin
kotlin-pune
kotlin-roadmap
kotlin-samples
kotlin-sap
kotlin-serbia
kotlin-spark
kotlin-szeged
kotlin-website
kotlinacademy
kotlinbot
kotlinconf
kotlindl
kotlinforbeginners
kotlingforbeginners
kotlinlondon
kotlinmad
kotlinprogrammers
kotlinsu
kotlintest
kotlintest-devs
kotlintlv
kotlinultimatechallenge
kotlinx-datetime
kotlinx-files
kotlinx-html
kotrix
kotson
kovenant
kprompt
kraph
krawler
kroto-plus
ksp
ktcc
ktfmt
ktlint
ktor
ktp
kubed
kug-leads
kug-torino
kvision
kweb
lambdaworld_cadiz
lanark
language-evolution
language-proposals
latvia
leakcanary
leedskotlinusergroup
lets-have-fun
libgdx
libkgd
library-development
lincheck
linkeddata
lithuania
london
losangeles
lottie
love
lychee
macedonia
machinelearningbawas
madrid
malaysia
mathematics
meetkotlin
memes
meta
metro-detroit
mexico
miami
micronaut
minnesota
minutest
mirror
mockk
moko
moldova
monsterpuzzle
montreal
moonbean
morocco
motionlayout
mpapt
mu
multiplatform
mumbai
munich
mvikotlin
mvrx
myndocs-oauth2-server
naming
navigation-architecture-component
nepal
new-mexico
new-zealand
newname
nigeria
nodejs
norway
npm-publish
nyc
oceania
ohio-kotlin-users
oldenburg
oolong
opensource
orbit-mvi
osgi
otpisani
package-search
pakistan
panamá
pattern-matching
pbandk
pdx
peru
philippines
phoenix
pinoy
pocketgitclient
polish
popkorn
portugal
practical-functional-programming
proguard
prozis-android-backup
pyhsikal
python
python-contributors
quasar
random
re
react
reaktive
realm
realworldkotlin
reductor
reduks
redux
redux-kotlin
refactoring-to-kotlin
reflect
refreshversions
reports
result
rethink
revolver
rhein-main
rocksdb
romania
room
rpi-pico
rsocket
russian
russian_feed
russian-kotlinasfirst
rx
rxjava
san-diego
science
scotland
scrcast
scrimage
script
scripting
seattle
serialization
server
sg-user-group
singapore
skia-wasm-interop-temp
skrape-it
slovak
snake
sofl-user-group
southafrica
spacemacs
spain
spanish
speaking
spek
spin
splitties
spotify-mobius
spring
spring-security
squarelibraries
stackoverflow
stacks
stayhungrystayfoolish
stdlib
stlouis
strife-discord-lib
strikt
students
stuttgart
sudan
swagger-gradle-codegen
swarm
sweden
swing
swiss-user-group
switzerland
talking-kotlin
tallinn
tampa
teamcity
tegal
tempe
tensorflow
terminal
test
testing
testtestest
texas
tgbotapi
thailand
tornadofx
touchlab-tools
training
tricity-kotlin-user-group
trójmiasto
truth
tunisia
turkey
turkiye
twitter-feed
uae
udacityindia
uk
ukrainian
uniflow
unkonf
uruguay
utah
uuid
vancouver
vankotlin
vertx
videos
vienna
vietnam
vim
vkug
vuejs
web-mpp
webassembly
webrtc
wimix_sentry
wwdc
zircon
Powered by
Title
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