Retrofit + RealmList + Gson陷入循环,直到内存不足 [英] Retrofit + RealmList + Gson stuck in a loop until out of memory

查看:266
本文介绍了Retrofit + RealmList + Gson陷入循环,直到内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我删除了RealmList对象,那么一切工作都可以正常进行,但我需要的对象列表。



Logcat:

 (背景粘性并发标记扫描GC释放278516(15MB)AllocSpace对象,0(0B)LOS对象,25%空闲,23MB / 31MB,暂停2.533ms总计1.049s 
(32099):背景部分并发标记扫描GC释放137132(8MB)AllocSpace对象,0(0B)LOS对象,40%免费,23MB / 39MB,暂停4.211ms总计188.951ms
(32099):背景粘性并发标记扫描GC释放271335(15MB) AllocSpace对象,0(0B)LOS对象,24%空闲,24MB / 32MB,暂停3.998ms总计236.868ms
(32099):背景粘性并发标记扫描GC释放143812(8MB)AllocSpace对象,0(0B) LOS对象,23%免费,24MB / 32MB,暂停6.470ms总计201.800ms
(32099):背景部分并发标记扫描GC已释放159223(9MB)AllocSp (0B)LOS对象,39%空闲,23MB / 39MB,暂停5.524ms总计215.809ms
(32099):背景粘性并发标记扫描GC释放278158(15MB)AllocSpace对象,0(0B) LOS对象,24%免费,24MB / 32MB,暂停1.922ms共201.617ms



Json: p>

  [
{
id:1,
title:子类别1,
color:FF0000,
tabs:[
{
id:1,
title:标签1

{
id:2,
title:标签2
}
]
$ bid:2,
title:子类别2,
color:DD0000,
tab:[
{
id:1,
title:Tab 1
},
{
ID:2,
title:Tab 2
}
]
}
]



子类别:

  public class子类别extends RealmObject {

@Serialize dName(id)
私人字符串ID;
@SerializedName(title)
私人字符串标题;
@SerializedName(color)
私有字符串颜色;

@SerializedName(tabs)
private RealmList< Tab>标签;

...套&获得...

}

标签类:

  public class Tab扩展RealmObject {

@SerializedName(id)
private String id;
@SerializedName(title)
私人字符串标题;

...套&获得...
}

RestClient使用:

  Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
。建立();

services = retrofit.create(Services.class);

services.getSubcategories()。enqueue(new Callback< RealmList< Subcategory>>(){
@Override
public void onResponse(Response< RealmList< Subcategory>>响应){
Log.e(TAG,Size:+ response.body()。size());
}

@Override
public void onFailure(Throwable t){
t.printStackTrace();
}
});

库:

  compile'io.realm:realm-android:0.87.0'
compile'c​​om.squareup.retrofit2:retrofit:2.0.0-beta3'
compile'c​​om.squareup.retrofit2 :converter-gson:2.0.0-beta3'


解决方案

您需要为GSON配置 ExclusionStrategy ,如下所述: https://realm.io/docs/java/latest/#gson

  Gson gson =新的GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy(){
@Override
public boolean shouldSkipField(FieldAttributes f){
return f.getDeclaringClass()。equals(RealmObject.class );
}

@Override
public boolean shouldSkipClass(Class<?> clazz){
return false;
}
} )
。创建();

Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();

更新:从Realm 0.89开始,不再需要定义排除策略,下面应该是:

  Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory (GsonConverterFactory.create())
.build();


I'm trying to user Retrofit + Realm + Gson, but when the RealmList is used the app get stuck.

If I remove the RealmList objects everything work's fine, but I need the object list.

Logcat:

(32099): Background sticky concurrent mark sweep GC freed 278516(15MB) AllocSpace objects, 0(0B) LOS objects, 25% free, 23MB/31MB, paused 2.533ms total 1.049s
(32099): Background partial concurrent mark sweep GC freed 137132(8MB) AllocSpace objects, 0(0B) LOS objects, 40% free, 23MB/39MB, paused 4.211ms total 188.951ms
(32099): Background sticky concurrent mark sweep GC freed 271335(15MB) AllocSpace objects, 0(0B) LOS objects, 24% free, 24MB/32MB, paused 3.998ms total 236.868ms
(32099): Background sticky concurrent mark sweep GC freed 143812(8MB) AllocSpace objects, 0(0B) LOS objects, 23% free, 24MB/32MB, paused 6.470ms total 201.800ms
(32099): Background partial concurrent mark sweep GC freed 159223(9MB) AllocSpace objects, 0(0B) LOS objects, 39% free, 23MB/39MB, paused 5.524ms total 215.809ms
(32099): Background sticky concurrent mark sweep GC freed 278158(15MB) AllocSpace objects, 0(0B) LOS objects, 24% free, 24MB/32MB, paused 1.922ms total 201.617ms

Json:

[
{
  "id": "1",
  "title": "Subcategory 1",
  "color": "FF0000",
  "tabs": [
    {
      "id": "1",
      "title": "Tab 1"
    },
    {
      "id": "2",
      "title": "Tab 2"
    }
  ]
},
{
  "id": "2",
  "title": "Subcategory 2",
  "color": "DD0000",
  "tabs": [
    {
      "id": "1",
      "title": "Tab 1"
    },
    {
      "id": "2",
      "title": "Tab 2"
    }
  ]
}
]

Subcategory class:

public class Subcategory extends RealmObject {

@SerializedName("id")
private String id;
@SerializedName("title")
private String title;
@SerializedName("color")
private String color;

@SerializedName("tabs")
private RealmList<Tab> tabs;

... sets & gets ...

}

Tab class:

public class Tab extends RealmObject {

@SerializedName("id")
private String id;
@SerializedName("title")
private String title;

... sets & gets ...
}

RestClient use:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

services = retrofit.create(Services.class);

services.getSubcategories().enqueue(new Callback<RealmList<Subcategory>>() {
        @Override
        public void onResponse(Response<RealmList<Subcategory>> response) {
            Log.e(TAG, "Size: " + response.body().size());
        }

        @Override
        public void onFailure(Throwable t) {
            t.printStackTrace();
        }
    });

Libs:

compile 'io.realm:realm-android:0.87.0'
compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'

解决方案

You need to configure an ExclusionStrategy for GSON as described here: https://realm.io/docs/java/latest/#gson

Gson gson = new GsonBuilder()
        .setExclusionStrategies(new ExclusionStrategy() {
            @Override
            public boolean shouldSkipField(FieldAttributes f) {
                return f.getDeclaringClass().equals(RealmObject.class);
            }

            @Override
            public boolean shouldSkipClass(Class<?> clazz) {
                return false;
            }
        })
        .create();

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

Update: From Realm 0.89 it should no longer be necessary to define the exclusion strategy, the below should be enough:

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

这篇关于Retrofit + RealmList + Gson陷入循环,直到内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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