我正在尝试使用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
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"
}
]
}
]
子类别类:
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 ...
}
标签类:
public class Tab extends RealmObject {
@SerializedName("id")
private String id;
@SerializedName("title")
private String title;
... sets & gets ...
}
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();
}
});
利布斯:
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'
答案 0 :(得分:14)
您需要为GSON配置ExclusionStrategy
,如下所述: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();
更新:从Realm 0.89开始,不再需要定义排除策略,下面应该足够了:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
答案 1 :(得分:2)
我面临同样的问题,尽管领域版本高于0.87,但我接受的答案并不适合我。我发布这个以防其他任何人面临这个问题并且接受的答案都不起作用。
我通过使用RealmList<MyModel>
函数将List<MyModel>
转换为ream.copyFromRealm
来实现它,该函数创建了域对象的内存副本。
这样的事情应该有效:
RealmList<MyModel> myModelRealmObjects = realmDBManager.getModelObjects();
List<MyModel> myModelObjects = realm.copyFromRealm(myModelRealmObjects);
然后通过myModelObjects
进行改造。