简体   繁体   中英

Filter parent and child at Realm - Android

I have two classes that I mapped as RealmObject and I would like to do a query that will filter both the parent and the child.

The query will filter all the products that are greater than the passed date and inside it filter all the compras that have date greater than the passed date.

Is it possible with a query or I really need to execute the query for products and after take this List and remove the compras that I don't want?

public class Produto extends RealmObject implements Id{

@PrimaryKey
private Long id;

@Index
@Required
private String codigoBarras;

private String nome;
private String marca;
private String categoria;
private String subCategoria;
private Double quantidade;
private String unidade;
private byte[] imagemData;
private Date dataAlteracao;

private RealmList<Compra> compras;

...


public class Compra extends RealmObject implements Id{

@PrimaryKey
private Long id;

//@LinkingObjects("compras")
private Produto produto = null;

private Double preco;
private String local;
private String mercado;
private Date data;
private Boolean liquidacao = false;
private String observacao;

private Date dataAlteracao;

...


public List<Produto> buscarProdutoEComprasPorDataAlteracao(Long dataAlteracao) {
    RealmResults<Produto> results = realm.where(Produto.class)
            .greaterThan("dataAlteracao", new Date(dataAlteracao))
            .greaterThan("compras.dataAlteracao", new Date(dataAlteracao))
            .sort("codigoBarras")
            .findAll();
    return realm.copyFromRealm(results);
}
//@LinkingObjects("compras") private Produto produto = null;

You can replace this with

@LinkingObjects("compras")
private final RealmResults<Produto> isComprasOfProdutos = null;

Although if your current query doesn't work, unfortunately Realm-Java does not support SUBQUERY nor the ALL predicate, and https://github.com/realm/realm-java/issues/5730 was never added nor do I think they will ever add it, so you'll have to do this manually. :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM