Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RETROFIT POST Realm object

I have the following RETROFIT API:

@POST("/payments")    
Observable<Response> saveCreditCard(@Body CreditCard creditCard)

CreditCard is a RealmObject.

When I try to use my API method:

CreditCard card = realm.createObject(CreditCard.class);
card.setWhateverField(...);
...
mApi.saveCreditCard(card)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(...);

I get the following error:

> 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.

I am assuming that RETROFIT is doing the serialisation to JSON on the io() scheduler, hence the error.

Does anyone have any suggestion how I can overcome Realm's threading problem?

like image 290
Tudor Luca Avatar asked May 19 '15 06:05

Tudor Luca


1 Answers

UPDATE

Realm added support to detach objects by using realm.copyFromRealm(yourObject, depthLevel)

CreditCard creditCard = realm.createObject(CreditCard.class);
card.setWhateverField(...);
...

final int relationshipsDepthLevel = 0;
creditCard = realm.copyFromRealm(creditCard, relationshipsDepthLevel);
mApi.saveCreditCard(temporaryCard)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(...);

DEPRECATED ANSWER follows:

I found a workaround that requires 2 extra lines of code, and an extra serialisation step.

@Inject
ObjectMapper mObjectMapper; // I use Dagger2 for DI

....

CreditCard creditCard = realm.createObject(CreditCard.class);
card.setWhateverField(...);
...
// I use Jackson's ObjectMapper to "copy" the original creditCard
// to a new temporary instance that has not been tied to a Realm.
String json = mObjectMapper.writeValueAsString(creditCard);
PaymentCreditCardDataView temporaryCard = mObjectMapper
                    .reader(PaymentCreditCardDataView.class)
                    .readValue(json);
mApi.saveCreditCard(temporaryCard)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(...);

The downside is that I have an extra object and an extra serialisation+deserialisation step, on the UI thread. It should be ok if I have objects with reasonable sizes.

like image 184
Tudor Luca Avatar answered Oct 21 '22 04:10

Tudor Luca