Android出现领域错误,尝试使用异步.说它是从没有弯针的线程打开的

我在此代码中收到以下错误:
在此行中,“您的领域是从没有Looper的线程中打开的.异步查询需要一个Handler来发送查询结果”.

"RealmResults<UserVehicle> completed = realm.where(UserVehicle.class).equalTo("id", userVehicleID).findAllAsync();"

我尝试在Handler(Looper.getMain())内设置realm.executeTransaction,但是没有运气.我究竟做错了什么?

PSVehicleService.getInstance(PSVehicleDetailsActivity.this).fetchVehicleTrips(userVehicle, fiveWeeksBeforeDate, lastTripDate, userID, new JsonCallback() {
            @Override
            public void onResponse(final JSONObject jsonObject, VolleyError error) {
                if(jsonObject != null) {
                    Log.i("","testVehicles maybeFetchData 11");
                    realm.executeTransaction(new Realm.Transaction() {
                        @Override
                        public void execute(Realm realm) {
                            try {
                                ObjectMapper mapper = new ObjectMapper();
                                final List<Trip> trips = mapper.readValue(jsonObject.getJSONArray("trips").toString(), new TypeReference<List<Trip>>() {
                                });
                                Log.i("","testVehicles maybeFetchData 12");
                                RealmResults<UserVehicle> completed = realm.where(UserVehicle.class).equalTo("id", userVehicleID).findAllAsync();
                                UserVehicle userVehicle = null;
                                if(completed.size() > 0){
                                    userVehicle = completed.get(0);
                                }
                                if(userVehicle != null) {
                                    Log.i("", "testVehicles maybeFetchData 13");
                                    userVehicle.getTrips().clear();
                                    Log.i("", "testVehicles maybeFetchData 14");
                                    userVehicle.getTrips().addAll(trips);
                                    Log.i("", "testVehicles maybeFetchData 15");
                                    realm.copyToRealmOrUpdate(userVehicle);
                                    Log.i("", "testVehicles maybeFetchData 16");
                                }else{
                                    Log.i("", "testVehicles maybeFetchData 16 NULLLLLLLL");
                                }
                            } catch (Exception e) {
                                Log.i("", "fetchTripsSinceWeeksInPast fetchVehicleTrips2 error" + e.getMessage());
                                Utils.appendLog("ERROR fetchVehicleTrips is:" + e.getMessage(), true);
                            }
                        }
                    }, new Realm.Transaction.Callback() {
                        @Override
                        public void onSuccess() {
                            Log.i("","testVehicles maybeFetchData 17");
                            setPager();
                            Log.i("","testVehicles maybeFetchData 18");
                        }

                        @Override
                        public void one rror(Exception e) {
                            Log.i("","testVehicles maybeFetchData 18 ERROR:" + e.getMessage());
                        }
                    });
                }
            }
        });

解决方法:

问题是您正在异步事务中使用异步查询-根本没有必要.

调用realm.executeTransaction(Realm.Transaction,Realm.Transaction.Callback)时,事务中的代码块将在没有循环程序且不需要循环程序的工作线程中运行.

但是对于异步查询,您需要一个循环程序以确保查询结果可以在Android上传递回去.

因此,要解决您的问题,只需简单地使用同步查询,而让它在事务的工作线程中运行即可.如:

RealmResults<UserVehicle> completed = realm.where(UserVehicle.class).equalTo("id", userVehicleID).findAll();
上一篇:android-服务,观察者和循环播放器


下一篇:Handler源码分析