HiroyukiTamura / realm-koroutines

Realm integration with kotlin coroutines

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Download

Realm Coroutines

A collection of convenience extension functions for realm database.

Usage

launch {
    // main thread
    val realm = Realm.getDefaultInstance()
    realm.transactAwait {
        // Different thread
        val testObject = TestObject(name = "Some Test")
        it.copyToRealm(testObject)
    }
    
    val result = realm.where<TestObject>().awaitFirst()
    ...
}

Offline Objects Support

Realm objects queried from the db cannot be used in different threads without explicitly disconnecting the objects as follows:

val realm = Realm.getDefaultInstance()
val result = realm.where<TestObject>().findFirst()
val offlineResult = realm.copyFromRealm(result)
launch(Dispatchers.IO) {
    // Do something with offlineResult
}

Now with the added offline functions, it can be done as follows:

val realm = Realm.getDefaultInstance()
val result = realm.where<TestObject>().firstOffline()
launch(Dispatchers.IO) {
    // Do something with result
}

With flows:

 launch(Dispatchers.IO) {
    val realm = Realm.getDefaultInstance()
    val resultsFlow = realm.where<TestObject>().flowAll()
    val testObjects = resultsFlow.take(1).first()
 }
 

Download

implementation 'com.michaelbukachi:realmkoroutines:0.1.4'

License

Copyright (C) 2019 Michael Bukachi

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
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

Realm integration with kotlin coroutines

License:Apache License 2.0


Languages

Language:Kotlin 86.4%Language:Java 13.6%