领域从错误的线程Android访问 [英] Realm access from incorrect thread Android

查看:111
本文介绍了领域从错误的线程Android访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上处理此问题:

I am dealing with this issue on Android :

从不正确的线程访问Realm。 Realm对象只能在它们创建的线程上访问。

Realm access from incorrect thread. Realm objects can only be accessed on the thread they where created.

我想在我的RemoteViewsFactory中使用Realm

I want to use Realm in my RemoteViewsFactory with

public class RemoteViewsX implements RemoteViewsFactory {

    public RemoteViews getViewAt(int paramInt) {
    if (this.results != null && this.results.size() != 0 && this.results.get(paramInt) != null) {
    //FAILED HERE
    }

}

...
此调用失败!为什么?

... This call failed! Why ?

我在课堂上获取这样的数据:

I fetch my data like this in my class:

public void onDataSetChanged() {
        Realm realm = Realm.getInstance(RemoteViewsX.this.ctx);
        this.results =  realm.where(Model.class).findAll();
}

我这样调用了我的remoteFactory:

I called my remoteFactory like this :

public class ScrollWidgetService extends RemoteViewsService {
    @Override
    public RemoteViewsFactory onGetViewFactory(Intent intent) {
        return new RemoteViewsX (getApplicationContext());
    }
}

任何想法?

推荐答案

如果问题是由调用 onDataSetChanged getViewAt 从另一个线程,你可以强制他们使用相同的线程,创建你自己的 HandlerThread ,如下所示:

If the problem is caused by calling onDataSetChanged and getViewAt from another thread, you can force them to use the same thread, creating your own HandlerThread like this:

public class Lock {
    private boolean isLocked;

    public synchronized void lock() throws InterruptedException {
        isLocked = true;
        while (isLocked) {
            wait();
        }
    }

    public synchronized void unlock() {
        isLocked = false;
        notify();
    }
}

public class MyHandlerThread extends HandlerThread {
    private Handler mHandler;

    public MyHandlerThread() {
        super("MY_HANDLER_THREAD");
        start();
        mHandler = new Handler(getLooper());
    }

    public Handler getHandler() {
        return mHandler;
    }
}

public class RemoteViewsX implements RemoteViewsFactory {
    private MyHandlerThread mHandlerThread;
    ...
}

public void onDataSetChanged() {
    Lock lock = new Lock();
    mHandlerThread.getHandler().post(new Runnable() {
        @Override
        public void run() {
            Realm realm = Realm.getInstance(ctx);
            results = realm.where(Model.class).findAll();
            lock.unlock();
        }
    });
    lock.lock();
}

public RemoteViews getViewAt(int paramInt) {
    Lock lock = new Lock();
    final RemoteViews[] result = {null};
    mHandlerThread.getHandler().post(new Runnable() {
        @Override
        public void run() {
            // You can safely access results here.
            result[0] = new RemoteViews();
            lock.unlock();
        }
    });
    lock.lock();
    return result[0];
}

我复制了 Lock 本页的课程: http://tutorials.jenkov.com/java-concurrency/locks。 html

当你的任务完成时,不要忘记退出处理程序线程。

I copied Lock class from this page: http://tutorials.jenkov.com/java-concurrency/locks.html
Do not forget to quit the handler thread when your tasks are done.

这篇关于领域从错误的线程Android访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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