Pause an Asynctask until a task is completed

Asked

Viewed 53 times

1

In my Asynctask I urge 4 new objects, only I have to stay in it until all objects are brought in. Follow the code:

@Override
protected Void doInBackground(Void... voids) {

    new CrtlComentario(contexto).trazer(post.getCodigo(), new CallbackTrazer() {
        @Override
        public void resultadoTrazer(Object obj) {
            c = (Comentario) obj;
            flag[3] = false;
        }

        @Override
        public void falha() {
            flag[3] = false;
        }

    });

    new CrtlUsuario(contexto).trazer(c.getUsuarioPost(), new CallbackTrazer() {
        @Override
        public void resultadoTrazer(Object obj) {
            u = (Usuario) obj;
            flag[0] = false;
        }

        @Override
        public void falha() {
            flag[0] = false;
        }
    });

    new CrtlComentarioPost(contexto).listar(new CallbackListar() {
        @Override
        public void resultadoListar(List<Object> lista) {
            for (Object obj : lista)
                cp.add((ComentarioPost) obj);
            flag[1] = false;
        }

        @Override
        public void falha() {
            flag[1] = false;
        }
    });

    new CrtlCurtidaComentario(contexto).listar(new CallbackListar() {
        @Override
        public void resultadoListar(List<Object> lista) {
            for (Object obj : lista)
                cc.add((CurtidaComentario) obj);
            flag[2] = false;
        }

        @Override
        public void falha() {
            flag[2] = false;
        }
    });

    return null;

}

The only objects that can never be null are "c" and "u" the other two can.

I need "c" to be loaded so I can load the other three, because I need your code.

I tried to do a while to stay until they end up using the flag variable[], but it doesn’t work and sometimes doesn’t even run the task.

1 answer

1

The fact that both the method trazer() and listar() receive a Callback gives idea that they are asynchronous.

If so it is not necessary to use a Asynctask.

Since you need the first object to "bring" the second, what you must do is "bring" the second in the method resultadoTrazer() of Callback of the first.

Anything like that:

new CrtlComentario(contexto).trazer(post.getCodigo(), new CallbackTrazer() {
    @Override
    public void resultadoTrazer(Object obj) {
        c = (Comentario) obj;

        //*** Talvez colocar em um método **************

        new CrtlUsuario(contexto).trazer(c.getUsuarioPost(), new CallbackTrazer() {
            @Override
            public void resultadoTrazer(Object obj) {
                u = (Usuario) obj;

            }

            @Override
            public void falha() {
                //Tratar a falha
            }
        });

        //*********************
    }

    @Override
    public void falha() {
        //Tratar a falha
    }

});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.