'Object update is not committed to realm when doing it within a write transaction

First of all sorry for my english.

I've been working with android a few months. I'm developing an app that will be used to check the articles within a rute are downloaded from the truck when arrives at the destinantion. When the destination is completed is marked as complete.

The first time I load the rute, mark the articles downloaded and export the rute to the database all works fine. My problem comes if I try to load the same rute again, because i want to mark the destination as complete because all of it's articles where downloaded before but the destination is not updated. The code that is not working is "seccion.setXcompleta(true);"

private void actualizarDestinos(ArrayList<String> secs) {
        for (int i = 0; i < secs.size(); i++) {
            RealmResults<CabeceraPedido> cabeceraPedidos = realm.where(CabeceraPedido.class).equalTo("xpv_destino", secs.get(i)).findAll();
            if (!cabeceraPedidos.isEmpty()) {
                ArrayList<Integer> numdoc_ids = new ArrayList<>();
                for (int j = 0; j < cabeceraPedidos.size(); j++) {
                    for (int c = 0; c < cabeceraPedidos.size(); c++) {
                        numdoc_ids.add(cabeceraPedidos.get(c).getXnumdoc_id());
                    }

                    RealmResults<LineaPedido> lineasPedido = realm.where(LineaPedido.class).in("xnumdoc_id", numdoc_ids.toArray(new Integer[0])).findAll();
                    Seccion seccion = realm.where(Seccion.class).equalTo("xseccion_id", cabeceraPedidos.get(j).getXpv_destino()).findFirst();
                    if (lineasPedido.isEmpty()) {
                        realm.beginTransaction();
                        seccion.setXcompleta(true);
                        cabeceraPedidos.get(j).setxDescargaCompleta(true);
                        realm.copyToRealmOrUpdate(seccion);
                        realm.commitTransaction();
                    } else {
                        realm.beginTransaction();
                        seccion.setXcompleta(false);
                        cabeceraPedidos.get(j).setxDescargaCompleta(false);
                        realm.copyToRealmOrUpdate(seccion);
                        realm.commitTransaction();
                    }
                }
            }
        }
    }

As is said, when I do it the first time I load the rute the destination is marked as complete when all the articles are dowloaded from the truck.

UPDATE

"Seccion" is a class that extends RealmObject. It is used to store the destinations on a rute. By default, when the rute is loaded the section is not complete because any of the articles contained on it are downloaded.

public class Seccion extends RealmObject {

    @PrimaryKey
    private String xseccion_id;
    private String xnombre;
    private String xgrupo_des;
    private String xlocal_act;
    private String xclave;
    private boolean xactual;
    private boolean xcompleta;

    public Seccion() {

    }

    public Seccion(String xseccion_id, String xnombre, String xgrupo_des, String xlocal_act, String xclave, boolean xactual) {
        this.xseccion_id = xseccion_id;
        this.xnombre = xnombre;
        this.xgrupo_des = xgrupo_des;
        this.xlocal_act = xlocal_act;
        this.xclave = xclave;
        this.xactual = xactual;
        this.xcompleta = false;
    }
}

Every section have one or more order headers (cabeceraPedido), and every order header have one or more order lines (lineasPedido). The image below shows one destination with four order headers (the order headers are not shown directly) with some order lines.

Destination section loaded incomplete

When all the articles from an order header are downloaded is checked as completed with cabeceraPedidos.get(j).setxDescargaCompleta(true), and when all the order headers are checked as completed the section is also checked as completed seccion.setXcompleta(true). At the UI the destination is highligthed in green.

Destination section checked as complete

At this point, on realm every order header and section are checked as complete. Now I export (EXPORTAR button) the data to database.

Now is when my problem comes. I load the same rute again, with the same destination and order headers but the order lines are not loaded because the articles where downloaded before. Every order header are checked as completed in actualizarDestinos method on the if clause but not the section, so i cannot highlight it on green in my recyclerview.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source