Search code examples
androidbackendless

load data from Backendless in chunks (small parts)


I am trying to figure out the answers of my questions , I'm fetching data from backendless asynchronously.

CODE:

BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setPageSize(10); // here am setting the page size (10 items to load at a single fetch )
dataQuery.setWhereClause(whereClause);

Backendless.Data.of(Note.class).find(dataQuery, new AsyncCallback<BackendlessCollection<Note>>() {
    @Override
    public void handleResponse(BackendlessCollection<Note> notes) {
        //here i got the first 10 objects from Note Table
    }

    @Override
    public void handleFault(BackendlessFault fault) {
        swipeToReload.setRefreshing(false);
        Toast.makeText(getContext(), "" + fault.getMessage(), Toast.LENGTH_LONG).show();
    }
});

So now I wanna know how can I load data in chunks , there would be a Button as the footer of RecylerView (here am populating my fetched data) but I don't how am gonna do that , what am I picturing about this in my mind is that "U have to fetch the data by telling server. The index of desired data like on first request fetch the data from 1 to 10 on next request fetch the data from 11-20 and so on."

But what if we're requesting the data (its 3rd Request ) from 21-30 and some new data got created (in first indexes) then how we gonna load those? Do we have to load everything from the starting ?? Please correct me.


Solution

  • got it working :

    public class MyForm {
        final DataHandler dataHandler;
        BackendlessCollection<Note> data;
    
        MyForm() {
            dataHandler = new DataHandler(this);
        }
    
        void setData(BackendlessCollection<Note> data) {
            this.data = data;
            //render new set of data
        }
    
        //'Fetch Data' Button click handler
        public void fetchData() {
            BackendlessDataQuery dataQuery = new BackendlessDataQuery();
            dataQuery.setPageSize(10);
    
            Backendless.Data.of(Note.class).find(dataQuery, dataHandler);
        }
    
        //'Next Page' Button click handler
        public void fetchNext() {
            data.nextPage(dataHandler);
        }
    
        void setError(BackendlessFault fault) {
            //swipeToReload.setRefreshing(false);
            //Toast.makeText(getContext(), "" + fault.getMessage(), Toast.LENGTH_LONG).show();
        }
    
        class DataHandler implements AsyncCallback<BackendlessCollection<Note>> {
            final MyForm form;
    
            DataHandler(MyForm form) {
                this.form = form;
            }
    
            public void handleResponse(BackendlessCollection<Note> notes) {
                form.setData(notes);
            }
    
            public void handleFault(BackendlessFault fault) {
                form.setError(fault);
            }
        }
    }