Realm.io和异步查询 [英] Realm.io and asynchronous queries

查看:485
本文介绍了Realm.io和异步查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们开始在Android应用程式中使用Realm.io。但是我们不喜欢Realm.io API的是没有任何异步方式来查询数据库。在旧项目中,我们使用DbQuery和Sqlite,因此我们习惯于在线程或 AsyncTask 中进行数据库查询。但是相当令人不安的是,在所有示例中,查询是在 UiThread 中进行的。这不是对应用程序性能的坏事吗?我们试图在线程或AsyncTasks内部进行查询,但是当我们在UiThread中访问我们的模型对象时,我们得到一个错误,说我们不能在它们被查询的线程中访问RealObjects。下面是我们的示例代码:

We are starting to use Realm.io in an android app project. But something that we don't like with the Realm.io API is the absence of any asynchronous way to query the database. In old projects we used DbQuery and Sqlite, so we are used to make the database queries inside threads or AsyncTask's. But is quite disturbing see that in all the examples the queries are made in the UiThread. Isn't this a bad thing for the app performance?. We tried to make the queries inside threads or AsyncTasks but we get an error when we access our model objects back in the UiThread, saying that we cannot access RealObjects in threads that wasnt the ones where they was queried. Here is our example code:

service.getDatabaseHelper()
            .with(getActivity()) //Context that I use to instance the Realm instance
            .onFinishQuery( new DatabaseHelper.IQueryGetCallback<UserRealm>() {
                @Override
                public void onGetResult(UserRealm[] results) {
                    for(UserRealm aUser : results){
                        //Here is where it crashes
                        Log.d("Log","Username -> "+aUser.getName());
                    }
                }
            })
            .getUsersFromDb();
//.......

//In our "DAO" class
public void getUsersFromDb(){
    new GetQueryTask() {
        @Override
        public UserRealm[] onQueryReadyToBeExecuted(Realm realmInstance) {
            RealmQuery<UserRealm> query = realmInstance.where(UserRealm.class);
            RealmResults<UserRealm> result = query.findAll();
            // TODO hacer que devuelva un array

            ArrayList<UserRealm> users = new ArrayList<UserRealm>();
            for (UserRealm u : result) {
                //Here we can read the RealObject's fine
                users.add(u);
            }
            return users.toArray(new UserRealm[users.size()]);
        }
    }.execute();
}

//Our abstract task that wraps all the instantiation-transaction behaviour
public abstract class GetQueryTask<T extends RealmObject> extends AsyncTask<Void, Void, T[]> {
    @Override
    protected T[] doInBackground(Void... params) {
        //We tried to instantiate this class in several places, here
        //Send it as parameter through the AsyncTask
        //context is set in the with(Context ctx) method.
        Realm realm = Realm.getInstance(context);
        return onQueryReadyToBeExecuted(realm);
    }

    public abstract T[] onQueryReadyToBeExecuted(Realm realmInstance);

    @Override
    protected void onPostExecute(T[] result) {
        mCallback.onExecute(result);
    }
}



我们的主要关注的不是的代码,但如果它可以做的查询在UiThread作为Realm.io开发人员在他们的例子。

Our major concern isn't is the "bug" of this code, but if it's okay to do the queries in the UiThread as the Realm.io developers do in their examples. If is that the case, all this code to perform the queries asynchronously will not be neccesary any more.

推荐答案

Christian从Realm这里可以解决这个问题。 。
你是正确的,查询当前需要在他们使用的线程上运行,这可能会对UI线程性能产生负面影响。我们非常清楚这一点,并且正在积极努力使得可以跨线程移动查询结果。

Christian from Realm here. You are right that queries are currently required to run on the thread they are used on, which potentially can have a negative effect on the UI thread performance. We are however acutely aware of this and are actively working on making it possible to move query results across threads.

这说明Realm中的查询相当快,即使对于合理的大量数据,并且由于它只加载数据,你实际使用可以非常高效甚至在UI线程上运行。

That said queries in Realm are quite fast even for reasonable large amounts of data, and due to it only loading data you actually use can be very performant even when run on the UI thread.

我鼓励你试试它与任何数据大小你似乎合理,并测试你的性能(在较旧的设备上优选:))。它也可以做一些技巧,例如添加索引到键,使在背景线程,重复查询发送键到UI线程和重新查询他们在那里。这是一个bandaid,但会在某些情况下工作 - 不幸的是不是为列表。

I would encourage you to try it out with whatever data size you seem reasonable and test your performance (on a older device preferably :)). It is also possible to do tricks like adding indexes to keys, make the heavy query on a background thread, send keys to the UI thread and requery for them there. It is a bandaid but will work in some cases - Sadly not for lists though.

这篇关于Realm.io和异步查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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