我使用Realm进行数据库服务,使用Retrofit进行服务器服务.
我想从Realm获取数据并将其作为对象传递给Retrofit,但是我收到以下错误:
retrofit.RetrofitError: Realm access from incorrect thread. Realm objects can only be accessed on the thread they where created.
05-25 12:58:36.418 W/System.err﹕ at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:400)
05-25 12:58:36.418 W/System.err﹕ at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
05-25 12:58:36.418 W/System.err﹕ at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278)
05-25 12:58:36.418 W/System.err﹕ at retrofit.CallbackRunnable.run(CallbackRunnable.java:42)
05-25 12:58:36.418 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-25 12:58:36.418 W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-25 12:58:36.423 W/System.err﹕ at retrofit.Platform$Android$2$1.run(Platform.java:142)
05-25 12:58:36.423 W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
05-25 12:58:36.423 W/System.err﹕ Caused by: java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they where created.
05-25 12:58:36.423 W/System.err﹕ at io.realm.Realm.checkIfValid(Realm.java:192)
05-25 12:58:36.423 W/System.err﹕ at io.realm.DisabilitiesRealmProxy.isWheelchair(DisabilitiesRealmProxy.java:47)
05-25 12:58:36.423 W/System.err﹕ at io.realm.DisabilitiesRealmProxy.toString(DisabilitiesRealmProxy.java:238)
05-25 12:58:36.423 W/System.err﹕ at retrofit.RequestBuilder.setArguments(RequestBuilder.java:312)
05-25 12:58:36.423 W/System.err﹕ at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:298)
05-25 12:58:36.423 W/System.err﹕ ... 7 more
一切都从这里开始:
if (ConnectionUtils.isConnectionToInternetEnabled(this)) {
PlaceServerService.getPlaces("London", DatabaseAdapter.getDisabilities(this));
}
哪个调用此方法:
public static void getPlaces(String city, Disabilities disabilities) {
PlaceClient client = createService(PlaceClient.class);
client.getPlaces(city, disabilities, new Callback<PlaceResponse>() {
@Override
public void success(PlaceResponse placeResponse, Response response) {
EventBus.getDefault().post(placeResponse.getPlaces());
}
@Override
public void failure(RetrofitError error) {
error.printStackTrace();
}
});
}
和接口:
@FormUrlEncoded
@POST("/places/city")
void getPlaces(@Field("city") String city,
@Field("disabilities") Disabilities disabilities,
Callback<PlaceResponse> callback);
我没有看到,这里有这个线程问题在哪里?我只是将对象传递给Retrofit方法.也许问题是Disabilities对象扩展了RealmObject?
编辑:
但是,这个解决方案有效,为什么?
if (ConnectionUtils.isConnectionToInternetEnabled(this)) {
Disabilities disabilities = DatabaseAdapter.getDisabilities(this);
Disabilities disability = new Disabilities(
disabilities.isWheelchair(),
disabilities.isBlind(),
disabilities.isDeaf(),
disabilities.isOtherMovement());
PlaceServerService.getPlaces("London", disability);
}
解决方法:
Yeah Disabilities扩展了RealmObject并且是Realm的一部分.你可以做的是创建一个新对象:
Disabilities disabilities = new Disabilities();
然后将Disabilities中的内容放入新对象中.这是有效的,因为此对象尚未在Realm中.