我使用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);
}
答案 0 :(得分:3)
是的Disabilities
延伸RealmObject
并且是Realm
的一部分。你可以做的是创建一个新对象:
Disabilities disabilities = new Disabilities();
然后将Disabilities
内的内容放入新对象中。这是有效的,因为此对象尚未在Realm
中。