我正在为我database
的Android Application
. 但是当我尝试RealmChangeListener
从 a添加/删除时,Runnable
我得到了exception
.
java.lang.IllegalStateException:您无法从非 Looper 线程中删除/注册侦听器
如果我不使用 Runnable,它工作正常。以下是我的代码:
public void clearDatabase(final RealmChangeListener realmChangeListener) {
Realm realm = RealmManager.getNewInstance().getRealm();
realm.addChangeListener(realmChangeListener);
realm.beginTransaction();
realm.clear(RBooking.class);
realm.commitTransaction();
}
工作代码:
protected void clearData() {
mDatabaseManager.clearDataBase(new RealmChangeListener() {
@Override
public void onChange() {
TLog.d("Feb 23 : Database has been clean completed!");
}
});
}
编辑:更新不工作的代码:
protected void processLogout() {
Runnable runnable = new Runnable() {
@Override
public void run() {
Looper.prepare();
mDatabaseManager.clearDataBase(new RealmChangeListener() {
@Override
public void onChange() {
TLog.d("Feb 23 : Database has been clean completed!");
}
});
Looper.loop();
}
};
doTask(runnable); // excute the runnable
}
所以任何机构都可以帮助我使 RealmChangeListener 在 Runnable 上正确工作。谢谢你。