У меня есть следующий API RETROFIT:
@POST("/payments")
Observable<Response> saveCreditCard(@Body CreditCard creditCard)
CreditCard
является RealmObject
.
Когда я пытаюсь использовать свой метод API:
CreditCard card = realm.createObject(CreditCard.class);
card.setWhateverField(...);
...
mApi.saveCreditCard(card)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(...);
Я получаю следующую ошибку:
> retrofit.RetrofitError: com.fasterxml.jackson.databind.JsonMappingException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they where created.
System.err﹕ at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:400)
System.err﹕ at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220)
System.err﹕ at retrofit.RestAdapter$RestHandler$1.invoke(RestAdapter.java:265)
System.err﹕ at retrofit.RxSupport$2.run(RxSupport.java:55)
System.err﹕ at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
System.err﹕ at retrofit.Platform$Android$2$1.run(Platform.java:142)
System.err﹕ at java.lang.Thread.run(Thread.java:818)
System.err﹕ Caused by: java.lang.AssertionError: com.fasterxml.jackson.databind.JsonMappingException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they where created.
Я предполагаю, что RETROFIT выполняет сериализацию в JSON
в планировщике io()
, следовательно, ошибка.
Есть ли у кого-нибудь предложения, как я могу преодолеть проблему Threading Threading?