Android Kotlin领域Query +返回Bg线程上非托管项目的正确方法 [英] Android Kotlin Realm Proper Way of Query+ Return Unmanaged Items on Bg Thread

查看:171
本文介绍了Android Kotlin领域Query +返回Bg线程上非托管项目的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用领域(后台线程中的所有内容)查询和返回非托管结果的正确方法是什么?我正在像这样使用somethibf:

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:

  • 没有将realmQuery封装为可观察的正确方法,因为 没有这种作弊方法(至少我知道),无法在后台线程中打开realInstance,所以我需要使用这种假方法 可观察到的Observable.just(1).
  • 不是在第一张和第二张地图内打开和关闭Realm实例的最佳位置

  • 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).

建议版本:

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

推荐答案

这是将Realm对象变为非托管对象的方式:

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());

虽然这个答案是Java,但Kotlin答案距离我们只有半步之遥.

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

这篇关于Android Kotlin领域Query +返回Bg线程上非托管项目的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆