我正在尝试构建一个名为request的通用对象,该对象可以容纳与不同实体相关的请求。
例如,可以为健身班,员工班或联系人班创建请求。现在下面的代码检查
我们是否存在任何离线数据。 getNonSynced
函数将返回RealmResults
那些未同步的数据。
我正在将realmResults
添加到请求类的RealmList
中。创建请求后,我便使用GSON将其转换为json字符串,这就是我的应用崩溃并抛出Caused by: java.lang.StackOverflowError: stack size 8MB
用于获取离线数据和解析请求的功能
public <T extends RealmModel > void checkOfflineData(){
Realm realm = Realm.getDefaultInstance();
Set<Class<? extends RealmModel>> realmObjectClasses = realm.getConfiguration().getRealmObjectClasses();
for(Class modelClass: realmObjectClasses) {
// fetch non synced data
RealmResults realmResults = getNotSynced(realm,modelClass);
if(realmResults !=null && realmResults.size() > 0){
//build a request
Request<T> request = new Request<T>();
RealmList<T> realmList = new RealmList<T>();
//add results to list
realmList.addAll(realmResults.subList(0, realmResults.size()));
request.setRequestList(realmList);
}
}
// Parsing using GSON data
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Request.class, new RequestSerializer());
Gson gson = builder.create();
String data = gson.toJson(request);
}
请求类别
public class Request<T> {
private RealmList<T> requestList;
public RealmList<T> getRequestList() {
return requestList;
}
public void setRequestList(RealmList<T> requestList) {
this.requestList = requestList;
}
}
这可能是在GSON尝试序列化RealmList<T> requestList
时可能在尝试查找具体类型的时候发生的。我所寻求的只是对课程进行装箱并为T提供适当的课程类型的方法
错误
07-17 13:04:00.177 6076-6076/com.org.connectedhealth W/System.err: Caused by: java.lang.StackOverflowError: stack size 8MB
at java.lang.reflect.Field.get(Native Method)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.writeField(ReflectiveTypeAdapterFactory.java:138)
07-17 13:04:00.178 6076-6076/com.org.connectedhealth W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:208)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:145)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
07-17 13:04:00.179 6076-6076/com.org.connectedhealth W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69)
07-17 13:04:00.180 6076-6076/com.org.connectedhealth W/System.err: at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127)
我在这里尝试了一些事情,如果我明确提供特定于类型的对象,则gson不会抛出错误。
Request<T> request = new Request<T>();
RealmList<T> realmList = new RealmList<T>();
// explicitly providing fitness object
realmList.add((T)new Fitness());
request.setRequestList(realmList);
所以我认为我需要探索一种返回具有特定类型的领域结果的方法。
private <T extends RealmModel> RealmResults<T> getNotSynced(Realm realm , Class<T> modelClass) {
RealmResults<T> realmResults =null;
try{
realmResults= realm.where(modelClass).equalTo("syncInfo.syncStatus", false).findAll();
}catch (Exception e){
Log.e(TAG ,"Exception "+e.getMessage());
}
return realmResults;
}
答案 0 :(得分:0)
通过替换以下行以将领域结果转换为领域列表,它开始工作,因为realmResults.subList(0, realmResults.size())
返回List<T>
,而copyFromRealm
返回List<RealmModel>
来自
realmList.addAll(realmResults.subList(0, realmResults.size()));
收件人
realmList.addAll(realm.copyFromRealm(realmResults));