Skip to content

Instantly share code, notes, and snippets.

Avatar

Christian Melchior cmelchior

View GitHub Profile
@cmelchior
cmelchior / VersionPinning.kt
Created July 16, 2021 11:27
Realm Kotlin 0.40 Release Blog Post - Version Pinning
View VersionPinning.kt
// BAD: Store a global managed object
MyApp.GLOBAL_OBJECT = realm.objects(Person::class).first()
// BETTER: Copy data out into an unmanaged object
val person = realm.objects(Person::class).first()
MyApp.GLOBAL_OBJECT = Person(person.name)
@cmelchior
cmelchior / VersionMismatch.kt
Created July 16, 2021 11:27
Realm Kotlin 0.40 Release Blog Post - Version Mismatch
View VersionMismatch.kt
// A write can now happen between two queries
val results1: RealmResults<Person> = realm.objects(Person::class)
val results2: RealmResults<Person> = realm.objects(Person::class)
// Resulting in subsequent queries not returning the same result
results1.version() != results2.version()
results1.size != results2.size
@cmelchior
cmelchior / ThreadsafeObservers.kt
Created July 16, 2021 11:25
Realm Kotlin 0.40 Release Blog Post - Threadsafe Observers
View ThreadsafeObservers.kt
val jane = getJane()
CoroutineScope(Dispatchers.Main).launch {
// Run mapping/transform logic in the background
val flow: Flow<String> = jane.observe()
.filter { it.name.startsWith("Jane") }
.flowOn(Dispatchers.Unconfined)
// Before collecting on the UI thread
flow.collect {
@cmelchior
cmelchior / ObservingChanges.kt
Created July 16, 2021 11:22
Realm Kotlin 0.40 Release Blog Post - Observing Changes
View ObservingChanges.kt
val jane = getJane()
CoroutineScope(Dispatchers.Main).launch {
// Updates are observed using Kotlin Flow
val flow: Flow<Person> = jane.observe()
flow.collect {
// Listen to changes to the object
println(it.name)
}
}
@cmelchior
cmelchior / UpdatingData.kt
Created July 16, 2021 11:21
Realm Kotlin 0.40 Release Blog Post - Updating Data
View UpdatingData.kt
CoroutineScope(Dispatchers.Main).launch {
// Create initial object
val jane = realm.write {
copyToRealm(Person("Jane"))
}
realm.write {
// Find latest version and update it
// Note, this always involves a null-check
// as another thread might have deleted the
@cmelchior
cmelchior / SuspendableWrite.kt
Created July 16, 2021 11:20
Realm Kotlin 0.40 Release Blog Post - Suspendable Write
View SuspendableWrite.kt
CoroutineScope(Dispatchers.Main).launch {
// Write automatically happens on a background dispatcher
val jane = realm.write {
val unmanaged = Person("Jane")
// Add unmanaged objects
copyToRealm(unmanaged)
}
// Objects returned from writes are automatically frozen
@cmelchior
cmelchior / BlockingWrite.kt
Created July 16, 2021 11:19
Realm Kotlin 0.40 Release Blog Post - Blocking Write
View BlockingWrite.kt
val jane = realm.writeBlocking {
val unmanaged = Person("Jane")
copyToRealm(unmanaged)
}
@cmelchior
cmelchior / ClosingRealms.kt
Created July 16, 2021 11:18
Realm Kotlin 0.40 Release Blog Post - Closing Realms
View ClosingRealms.kt
// Close Realm to free native resources
realm.close()
@cmelchior
cmelchior / OpeningRealms.kt
Created July 16, 2021 11:17
Realm Kotlin 0.40 Release Blog Post - Opening Realms
View OpeningRealms.kt
// Global App variable
class MyApp: Application() {
companion object {
private val config = RealmConfiguration(schema = setOf(Person::class))
public val REALM = Realm(config)
}
}
// Using dependency injection
val koinModule = module {
View gist:3fe791f84db37fd3bcb3749d4188168a
/**
* Class connecting the Realm lifecycle to that of LiveData objects.
* Realm will remain open for as long as any LiveData objects are being observed.
*/
abstract class LiveRealmData<T: RealmModel>(val config: RealmConfiguration) : LiveData<RealmResults<T>>() {
private val listener = RealmChangeListener<RealmResults<T>> { results -> value = results }
private lateinit var realm: Realm
private var results: RealmResults<T>? = null