Android has a domain error and tries to use asynchrony. It says it is opened from a thread without a looper
I receive the following error in this Code: in this line, "your domain is opened from a thread without looper. Asynchronous query requires a handler to send query results"
"RealmResults<UserVehicle> completed = realm.where(UserVehicle.class).equalTo("id", userVehicleID).findAllAsync();"
I tried to set realm.executetransaction in the handler (looper. Getmain()), but I had no luck. What did I do wrong?
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());
}
});
}
}
});
resolvent:
The problem is that you are using asynchronous queries in asynchronous transactions - not necessary at all
When calling realm. Executetransaction (realm. Transaction, realm. Transaction. Callback), the code block in the transaction will run in the worker thread that does not have a looper and does not need a looper
But for asynchronous queries, you need a loop program to ensure that the query results can be passed back on Android
Therefore, to solve your problem, simply use synchronous query and let it run in the working thread of the transaction. For example:
RealmResults<UserVehicle> completed = realm.where(UserVehicle.class).equalTo("id", userVehicleID).findAll();