【发布时间】:2019-02-06 17:50:07
【问题描述】:
我有以下数据结构,包含在供应商列表中,有产品,每个产品都有不同的类型
提供者类
public class Provider extends RealmObject {
@SerializedName("id")
@PrimaryKey
public int providerId;
public String name;
public RealmList<Product> products;
}
产品类别
public class Product extends RealmObject {
@SerializedName("id")
@PrimaryKey
public int productId;
public String name;
public RealmList<ProductType> types;
}
产品类型类
public class ProductType extends RealmObject {
@SerializedName("id")
@PrimaryKey
public int productTypeId;
public String name;
public Packaging packaging; }
这是加载到领域的 JSON 示例:
{
"providers": [{
"id": 1,
"name": "PROVIDER_1",
"products": [{
"id": 10,
"name": "Banana",
"types": [{
"id": 101,
"name": "Costa rica",
"packaging": {
"isFree": true,
"value": 0
}
},
{
"id": 102,
"name": "Brasil",
"packaging": {
"isFree": true,
"value": 0
}
}
]
}]
},{
"id": 4,
"name": "PROVIDER_2",
"products": [{
"id": 10,
"name": "Banana",
"types": [{
"id": 103,
"name": "Ecuador Prem",
"packaging": {
"isFree": true,
"value": 0
}
}
]
},
{
"id": 21,
"name": "Apple",
"types": [{
"id": 212,
"name": "Red del",
"packaging": {
"isFree": true,
"value": 0
}
}
]
}]
}
]
}
所以,当我尝试使用以下查询获取香蕉类型时出现了我遇到的问题
RealmResults<Provider> results = mRealm.where(Provider.class).equalTo("providerId", providerId).findAll();
Product product = results.where().equalTo("providerId", providerId).findFirst().products.where().equalTo("productId", productId).findFirst();
RealmList<ProductType> types = product.types;
返回的类型列表总是给我第二个提供者的类型。在当前示例中,即使我请求提供者 ID 1(即 PROVIDER_1),我也会得到“厄瓜多尔普雷姆”,并且该提供者必须以“哥斯达黎加”和“巴西”类型返回。
【问题讨论】: