Hi all, coming from Swift development, I’m able to...
# announcements
w
Hi all, coming from Swift development, I’m able to store some user defaults / persist some small items using something called CoreData (for those who’ve not heard of it). What would the equivalent be for Kotlin/Android development to persist some user data?
r
You may want to ask in #C0B8M7BUY, but this question may be better suited for StackOverflow.
k
Well, one option would be sqldelight (which can be called from iOS as well #C3PQML5NU ) https://github.com/cashapp/sqldelight
w
That’s super helpful, Kevin. Thanks! @Ruckus - also looking around SO but enjoy engaging with the community live too, especially as so much of SO can be outdated. I hear you about the #C0B8M7BUY channel though
c
SharedPreferences is what you’d use for a simple key-value store, and SQLite for a more robust local datastore. SP has a multiplatform Android/iOS wrapper (https://github.com/russhwolf/multiplatform-settings). For using sqlite, you’d typically use Room (best with coroutine extensions) in an Android-only app, and sqldelight for multiplatform
k
Well, I think “typically” depends on what you’re looking for. Besides “multiplatform”, they’re different philosophically. Room is probably more similar to CoreData if you want an “ORM”. Sqldelight starts with sql and generates code from that direction. I would say many more teams pick based on that than multiplatform
💯 1
w
key-value pairs are pretty much all I’m looking to store at the moment, so maybe SharedPreferences is a low-cost thing to look into for that. I will want a way to store a potentially very long JSON string which might require something a little more SQLite-y
k
Key/value, def Shared prefs. The
multiplatform-settings
lib uses
NSUserDefaults
, which is sort of the iOS equivalent
w
Nice! Thanks @kpgalligan & @Casey Brooks , super helpful
c
No one mentioned Room? that would be closest to CoreData, though it’s more interested in maintaining a SQL-esque veneer…
oh nm Kevin mentioned it
k
Casey first. I was just randomly in general and thought I’d play the multiplatform card
w
Will look into Room too!
k
Yeah I was going to say Room. For very simple stuff (think: your settings menu’s values)
SharedPreferences
is the go-to, but
Room
is Google’s preferred persistence library. It’s a wrapper over Sqlite. It’s a very powerful library.
The only drawback I ever ran into is that if you write Room stuff in Kotlin, it necessarily interacts with Java code, and Java blows up in specific instances where it encounters Kotlin’s
inline class
. I had a bug for days where I couldn’t get my code to run (but it compiled!) and it was because I had my ID column as an inline class wrapping around a Long, and Java did not like this one bit.
w
Thanks @kyleg super helpful. I think it sounds as though, in this instead, using a combination of SharedPrefs for storing preferences/options, and Room for storing the larger user-created objects is probably the way to go. So appreciate everyone’s help!!
k
Yes, that’s what I do, @Will Nixon. In my weightlifting app, I change display unit (KG vs LB), for example, using SharedPreferences, but storing how much weight a user lifted for a given workout with a specific bar and # of reps etc. I store in a Sqlite DB with via Room.