View VersionPinning.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
View VersionMismatch.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
View ThreadsafeObservers.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
View ObservingChanges.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
View UpdatingData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
View SuspendableWrite.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
View BlockingWrite.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val jane = realm.writeBlocking { | |
val unmanaged = Person("Jane") | |
copyToRealm(unmanaged) | |
} |
View ClosingRealms.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Close Realm to free native resources | |
realm.close() |
View OpeningRealms.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 |
NewerOlder