Skip to content

Instantly share code, notes, and snippets.

View cmelchior's full-sized avatar

Christian Melchior cmelchior

View GitHub Profile
@cmelchior
cmelchior / LogCat
Last active April 3, 2017 19:48
Debugging Progress Listeners
04-03 15:44:04.233 8911-8947/io.realm.test E/ProgressUpdate: /data/data/io.realm.test/cache/junit-1429296718/custom-admin-user/tests/tests -> downloaded: 0,downloadable: 0,uploaded: 0,uploadable: 0, is_fresh 0
04-03 15:44:04.237 8911-8947/io.realm.test E/ProgressUpdate: /data/data/io.realm.test/cache/junit-1429296718/custom-admin-user/tests/tests -> downloaded: 0,downloadable: 0,uploaded: 0,uploadable: 0, is_fresh 0
04-03 15:44:04.276 8911-8947/io.realm.test E/ProgressUpdate: /data/data/io.realm.test/cache/junit-1429296718/custom-admin-user/tests/tests -> downloaded: 0,downloadable: 0,uploaded: 0,uploadable: 8844, is_fresh 0
04-03 15:44:04.279 8911-8947/io.realm.test E/ProgressUpdate: /data/data/io.realm.test/cache/junit-1429296718/custom-admin-user/tests/tests -> downloaded: 0,downloadable: 0,uploaded: 0,uploadable: 9187, is_fresh 0
04-03 15:44:04.289 8911-8947/io.realm.test E/ProgressUpdate: /data/data/io.realm.test/cache/junit-1429296718/custom-admin-user/tests/tests -> downloaded: 0,downloadable: 0,upload
@cmelchior
cmelchior / PrimaryKeyFactory.java
Created April 7, 2017 12:01
Helper class for creating auto increment keys for Realm model classes
/*
* Copyright 2017 Realm Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
/**
* 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
@cmelchior
cmelchior / OpeningRealms.kt
Created July 16, 2021 11:17
Realm Kotlin 0.40 Release Blog Post - Opening Realms
// 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 {
@cmelchior
cmelchior / ClosingRealms.kt
Created July 16, 2021 11:18
Realm Kotlin 0.40 Release Blog Post - Closing Realms
// Close Realm to free native resources
realm.close()
@cmelchior
cmelchior / BlockingWrite.kt
Created July 16, 2021 11:19
Realm Kotlin 0.40 Release Blog Post - Blocking Write
val jane = realm.writeBlocking {
val unmanaged = Person("Jane")
copyToRealm(unmanaged)
}
@cmelchior
cmelchior / SuspendableWrite.kt
Created July 16, 2021 11:20
Realm Kotlin 0.40 Release Blog Post - Suspendable Write
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 / UpdatingData.kt
Created July 16, 2021 11:21
Realm Kotlin 0.40 Release Blog Post - Updating Data
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 / ObservingChanges.kt
Created July 16, 2021 11:22
Realm Kotlin 0.40 Release Blog Post - Observing Changes
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 / ThreadsafeObservers.kt
Created July 16, 2021 11:25
Realm Kotlin 0.40 Release Blog Post - Threadsafe Observers
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 {