Android Kotlin Realm Proper Way of Query+ Return Unmanaged Items on Bg Thread

偶尔善良 提交于 2019-12-01 09:45:45

问题


What is the proper way of querying and returning an unmanaged result of items with realm, everything in the background thread?. I'm using somethibf like this:

return Observable.just(1)
                .subscribeOn(Schedulers.io())
                .map {
                    val realm = Realm.getDefaultInstance()
                    val results = realm.where(ItemRealm::class.java)
                            .equalTo("sent", false).findAll()

                    realm to results
                }
                .map {
                    val (realm, results) = it
                    val unManagedResults = realm.copyFromRealm(results)
                    realm.close()
                    unManagedResults
                }
    }

And then chaining this observable with another one that will post the results to a server.

The solution working, although is a bit ugly on this aspects:

  • No proper way of wrapping the realmQuery in an observable, because there is no way of opening a realInstance in a background thread without this kind of cheat (at least that i know about), so i need to use this fake observable Observable.just(1).
  • Not the best place to open and close Realm instances, inside first and second map

  • I don't know if it is guaranteed that the realm instance is closed after all the items have been copied.

So what is the proper way of Query and Return unmanaged results on the background thread (some context, i need this to send the results to a server, in the background and as this task is totally independent from my app current data flow, so it should be off the main thread).

Suggested Version:

return Observable.fromCallable {
            Realm.getDefaultInstance().use { realm ->
                realm.copyFromRealm(
                        realm.where(ItemRealm::class.java)
                                .equalTo(ItemRealm.FIELD_SEND, false).findAll()
                )
            }
        }

回答1:


This is how you would turn your Realm objects unmanaged:

    return Observable.defer(() -> {
        try(Realm realm = Realm.getDefaultInstance()) {
            return Observable.just(
                realm.copyFromRealm(
                    realm.where(ItemRealm.class).equalTo("sent", false).findAll()
                )
            );
        }
    }).subscribeOn(Schedulers.io());

Although this answer is Java, the Kotlin answer is just half step away.



来源:https://stackoverflow.com/questions/38981751/android-kotlin-realm-proper-way-of-query-return-unmanaged-items-on-bg-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!