我有一个片段,我正在使用这样的领域
String selectedLang;
Realm realm;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.user_profile, container, false);
ButterKnife.bind(this, rootView);
realm = Realm.getDefaultInstance();
loginDatas = realm.where(LoginData.class).findFirst();
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedLang = langList.get(position);
//tried this, still not working, added runOnUiThread and that also didn't work
/*Realm realGuys = Realm.getDefaultInstance();
realGuys.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
loginDatas.getUser().setLanguage(selectedLang);
realm.insertOrUpdate(loginDatas);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(UserFragment.this).attach(UserFragment.this).commit();
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
error.printStackTrace();
}
});*/
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
//getting the exception below here
loginDatas.getUser().setLanguage(selectedLang);
realm.insertOrUpdate(loginDatas);
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(UserFragment.this).attach(UserFragment.this).commit();
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
error.printStackTrace();
}
});
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
selectedLang = langList.get(0);
}
});
}
首先我从realm获取loginDatas,然后我有一个微调器设置,在onItemSelected中,我需要将所选项的值设置为loginDatas然后将其设置为realm。我已经设置了我的代码,我得到了这个异常
7:20.060 9708-9708/com.packagename W/System.err: java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
at io.realm.BaseRealm.checkIfValid(BaseRealm.java:426)
at io.realm.LoginDataRealmProxy.realmGet$user(LoginDataRealmProxy.java:99)
at com.comcast.comcastmobile.entity.LoginData.getUser(LoginData.java:22)
at com.comcast.comcastmobile.user.UserFragment$2$1.execute(UserFragment.java:189)
at io.realm.Realm$1.run(Realm.java:1502)
at io.realm.internal.async.BgPriorityRunnable.run(BgPriorityRunnable.java:34)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:0)
有点像你不能在后台线程上访问UI线程视图,你也无法访问在不同线程上获得的RealmObjects。
loginDatas
是从UI线程Realm获得的,因此您无法在正在执行异步事务的后台线程上访问它。您必须从后台线程Realm中找到loginDatas
以在后台线程上修改它。
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
LoginData loginDatas = realm.where(LoginData.class).findFirst();
loginDatas.getUser().setLanguage(selectedLang);
// realm.insertOrUpdate(loginDatas);
}