Realm和Retrofit2:发送自动管理的对象 [英] Realm and Retrofit2: sending auto-managed objects

查看:150
本文介绍了Realm和Retrofit2:发送自动管理的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Realm和Retrofit2将自动管理的RealmObjects发送到我们的服务器时,Retrofit2(使用Gson)只发送RealmObject中的ints。它完全忽略了字符串和其他字段,并没有把这些放在json中。



然而,如果我将RealmObject从realm中断开:

  realm.copyFromRealm(myRealmObject)

然后发送所有领域。可能是什么问题呢?是否有合适的解决方案?

解决方案

在我们潜水之前

在我的Stackoverflow的一篇帖子中,我已经解释了使用Gson和Realm时发生了什么(Retrofit只是使用Gson作为数据转换器,所以它是Gson谁没有改造)。

让我们深入


...只发送RealmObject中的整数

不可以!不只是 ints ...



如果仔细观察,您会注意到即使您的 ints 被设置为0(即 int null 值) 。对于布尔值也会发生同样的情况,您会在序列化输出中得到 false



事情是,所有的 RealmObject 属性都设置为 null 当这个相同的 realmObject 托管时。当您尝试读取/写入属性(来自托管的 realmObject )时,Realm将从持久层读取/写入其值(使用 proxies ),所以你确定你得到/设置了这个 realmObject (不只是从内存中获取旧值)。

现在我可以解释为什么Gson只是序列化 ints




  • 当您的属性为 object ,它的值将等于 null (一个指向无处的引用),G​​son不会打扰序列化它当你的属性是一个缩放器类型( char )时,它将不会在输出中看到它。

  • int boolean float ...)值将等于对应于 null (标量中的每一位都是0)的任何值,并且Gson将seriali它会导致它被认为是一个有效的价值。这解释了为什么只有你的 ints 被序列化。



然而,如果我将RealmObject从Realm中断开,那么
将发送所有字段。


realmObject 非托管时,它将充当一个普通的java对象(不使用代理来保持对象之间的一致性内存和持久化的),当然Gson也不会有序列化它的麻烦。


有没有适当的解决方案?


有解决方法。在我之前提到的文章中,我试图收集一些推荐的文章(IMO)来解决这种不兼容问题。以下是链接:Android:Realm + Retrofit 2 + GSON


When using Realm and Retrofit2 for sending auto-managed RealmObjects to our server, Retrofit2 (using Gson) only sends the ints in the RealmObject. It completely ignores the Strings and other fields, and does not put these in the json. No errors are logged.

If I however, disconnect the RealmObject from Realm:

realm.copyFromRealm(myRealmObject)

then it does send all fields. What could be the problem? Is there a proper solution?

解决方案

Before we dive in

In one of my posts here on Stackoverflow, I have explained what's happening when using Gson and Realm together (Retrofit is just using Gson as a data converter, so it's Gson who's failing not Retrofit). The link is posted down below.

Let's dive in

... only sends the ints in the RealmObject

Nope! Not just ints...

If you look closely, you'll notice that even your ints are set to 0 (which is the null value for an int). Same thing would happen with a boolean, you would get false in the serialized output.

The thing is that, all your RealmObject attributes are set to null when this same realmObject is managed. When you try to read/write an attribute (from a managed realmObject), Realm will read/write its value from/to the persistence layer (using proxies) so you're sure you're getting/setting the right value of this realmObject (not just getting an old value from the memory).

That being said, I can now explain why Gson is only serializing ints.

  • When your attribute is an object, its value will be equal to null (a reference pointing to nowhere) and Gson won't bother serializing it (you won't see it in the output).

  • When your attribute is a scaler type (char, int, boolean, float ...) its value will be equal to whatever corresponds to a null (every bit in the scalar is 0) and Gson will serialize it cause it's considered to be a valid value. This explains "why only your ints are serialized".

If I however, disconnect the RealmObject from Realm ... then it does send all fields.

When your realmObject is unmanaged it'll act a normal java object (no proxies are used to maintain the coherence between the object in memory and the persisted one) and of course Gson will have no trouble serializing it.

Is there a proper solution?

There are workarounds. In the post I mentioned earlier, I tried to gather some recommended ones (IMO) to deal this incompatibility. Here's the link: "Android: Realm + Retrofit 2 + GSON".

这篇关于Realm和Retrofit2:发送自动管理的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆