Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

addAll() method is not supported by RealmResults

What can I use instead of addAll() method in my adapter, I'm using realm version 2.0.1 and that method is deprecated, I'm trying to get all the data from the API, save it to my database and pass it to my adapter, I'm using like this:

  public void getData(int page) {
    if (GlobalModel.existeConexao()) {

        Call<PedidosResponse> call = ((NavigationMain) getActivity()).apiService.getPedidos(GlobalModel.getToken(), GlobalModel.geEmpresaId(), page);
        call.enqueue(new Callback<PedidosResponse>() {
            @Override
            public void onResponse(Call<PedidosResponse> call, Response<PedidosResponse> response) {
                if (response.isSuccessful()) {
                    for (int i = 0; i < response.body().getPedidos().size(); i++) {
                        Pedidos mPedido = response.body().getPedidos().get(i);
                        int myInt = (mPedido.isProjecao()) ? 1 : 0;
                        if (!mRepositorio.checkIfExists(mPedido.getId())) {
                            mRepositorio.addPedido(mPedido.getId(), mPedido.getCliente_id(), mPedido.getData_hora(), mPedido.getData_pedido_cliente(), mPedido.getPrevisao_entrega(), mPedido.getFrete_tipo(), myInt, mPedido.getObservacao(), mPedido.getAliquota_projecao(), mPedido.getStatus(), mPedido.getPedido_cliente());

                        }
                    }
                    arraypedidos = mRepositorio.findAllPedidos();

                    if (mPedidosAdapter == null) {
                        mPedidosAdapter = new PedidosAdapter(getActivity(), arraypedidos);
                        listpedidos.setAdapter(mPedidosAdapter);
                    } else {
                        mPedidosAdapter.setData(arraypedidos);
                    }


                }
            }

            @Override
            public void onFailure(Call<PedidosResponse> call, Throwable t) {
                if (t.getMessage() != null) {
                    Log.v("pedidos", t.getMessage());
                }

            }
        });

    } else {
        Toast.makeText(getActivity(), "Verifique sua conexão", Toast.LENGTH_SHORT).show();
    }

}

But when I run the app I get this message:

java.lang.UnsupportedOperationException: This method is not supported by RealmResults.
like image 383
AND4011002849 Avatar asked Oct 29 '25 15:10

AND4011002849


1 Answers

That's because RealmResults is just a set of pointers that satisfy the condition defined in the query. You can't manipulate it, nor should you if you just intend to show every element in your adapter.

In fact, Realm was explicitly designed to simplify the workflow of "downloading data on a background thread and saving the data in a database", and "showing the data downloaded on a background thread automatically on the UI thread".

This is what RealmChangeListener is for.

Simply put, all of this code is unnecessary:

                arraypedidos = mRepositorio.findAllPedidos();

                if (mPedidosAdapter == null) {
                    mPedidosAdapter = new PedidosAdapter(getActivity(), arraypedidos);
                    listpedidos.setAdapter(mPedidosAdapter);
                } else {
                    mPedidosAdapter.setData(arraypedidos);
                }

And could be replaced with this:

public class SomeActivity extends AppCompatActivity {
    PedidosAdapter pedidosAdapter;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.some_view);
        pedidosAdapter = new PedidosAdapter(context, mRepositorio.findAllPedidos());
        // set adapter, ...
    }
}

And

public class PedidosAdapter extends RealmRecyclerViewAdapter<Pedidos, PedidosViewHolder> {
    public PedidosAdapter(Context context, RealmResults<Pedidos> results) {
        super(context, results, true);
    }

    // onBindViewHolder

    // onCreateViewHolder
}

For this, use RealmRecyclerViewAdapter, unless you intend to handle the RealmChangeListener manually.

like image 97
EpicPandaForce Avatar answered Oct 31 '25 05:10

EpicPandaForce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!