Retrofit + RealmList + Gson陷入循环直到内存不足

Isq*_*rdo 5 android realm gson retrofit

我正在尝试使用Retrofit + Realm + Gson,但是当使用RealmList时,应用程序会卡住.

如果我删除RealmList对象一切正常,但我需要对象列表.

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
Run Code Online (Sandbox Code Playgroud)

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"
    }
  ]
}
]
Run Code Online (Sandbox Code Playgroud)

子类别:

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

}
Run Code Online (Sandbox Code Playgroud)

标签类:

public class Tab extends RealmObject {

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

... sets & gets ...
}
Run Code Online (Sandbox Code Playgroud)

RestClient使用:

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();
        }
    });
Run Code Online (Sandbox Code Playgroud)

库:

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'
Run Code Online (Sandbox Code Playgroud)

Chr*_*ior 14

您需要ExclusionStrategy按照此处的说明配置for GSON: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();
Run Code Online (Sandbox Code Playgroud)

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

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
Run Code Online (Sandbox Code Playgroud)

  • 升级到领域1.1.0并遇到与改造和gson相同的问题. (2认同)