接口为Retrofit
public interface MapApi {
@Headers({"Content-Type: application/json", "Cache-Control: max-age=640000"})
@POST("map/saveareas")
Call<StandardResponse> saveAreas(@Body List<Area> areas);
}
Pojo类区域
public class Area extends RealmObject {
@PrimaryKey
@Expose
private String id = UUID.randomUUID().toString();
@Expose
private String name;
@Expose
private String description;
@Expose
private String parentId;
@Expose
private int color;
@Expose
private Date date = new Date();
@Expose
private boolean favourite = false;
public Area(){}
//Другой конструктор и геттеры, сеттеры
}
所以,我得到一个对象列表Realm并将这个列表传递给改造
List<Area> areas = mRealm
.where(Area.class)
.findAll();
if (areas != null) {
try {
Call<StandardResponse> saveAreasResponseCall =
MyApplication.mMapApi.saveAreas(areas);
saveAreasResponseCall.enqueue(new Callback<StandardResponse>() {
@Override
public void onResponse(Call<StandardResponse> call, Response<StandardResponse> response) {
Log.d(MyApplication.TAG, CLASS + ": " + response.code());
if (response.code() == 200) {
StandardResponse standardResponse = response.body();
}
}
@Override
public void onFailure(Call<StandardResponse> call, Throwable t) {
Log.e(MyApplication.TAG, CLASS + ": " + t.getMessage());
}
});
} catch (Exception e) {
Log.e(MyApplication.TAG, CLASS + ": " + e.getMessage());
}
}
应用程序冻结并黑屏,30 秒后挂起并出现错误
E/tag: MapsFragment: stack size 8MB
但是,如果我自己创建它List<Area> areas1,用其中的数据手动填充它Realm并将其传递给改造,那么一切正常。例如,
List<Area> areas1 = new ArrayList<>();
areas1.add(new Area("eb1737a1-9693-47d9-8390-5d02a249f5c4", "test", "test description", "", -10647809, new Date("Mon Feb 05 22:37:35 GMT+10:00 2018"), false));
areas1.add(new Area("7017c01a-cb6b-403f-956a-38eeedfca598", "tes", "", "eb1737a1-9693-47d9-8390-5d02a249f5c4", -5349889, new Date("Mon Feb 05 22:38:11 GMT+10:00 2018"), false));
如果我尝试areas1.addAll(areas)再次使用从 获取的列表中传递数据Realm,然后将新列表传递给Retrofit,那么一切都会再次挂起。
为什么无法从
RealmRetrofit-y 传递列表?问题隐藏在GSON图书馆的使用中。准登免费翻译GSON为何无处不在
null当然,您可以
JsonSerializers为每个模型类创建一个自定义的,但不建议使用这种方法。这个问题有几种解决方案,这里是其中之一,在我看来是最实用的。
创建通用接口
我们在模型类中实现了这个接口。
公共类区域扩展 RealmObject 实现 CloneableRealmObject {
现在
Realm可以通过Retrofit首先调用此对象上的方法来传递从中获取的对象cloneableRealmObject()。资源