control an Automated function manually

别等时光非礼了梦想. 提交于 2019-12-25 08:48:07

问题


Well I apologize for not so specific and clear title (tried too hard for it ), so I have this function

public void fetchData(){
    dataQuery.setPageSize(10); // fetch 10 items per request 
    final boolean[] firstResponse = {true};
    final CountDownLatch latch = new CountDownLatch( 1 );

    // a callback of fetching data from server 
    Backendless.Data.of(Note.class).find(dataQuery, new AsyncCallback<BackendlessCollection<Note>>() {
        @Override
        public void handleResponse(BackendlessCollection<Note> notes) { // here we have the response of request 

             /// loop for  requesting for items until all of them is fetched////// 
            if(firstResponse[0])
            {
                firstResponse[0] =false;
            }
            int size = notes.getCurrentPage().size();
            if( size > 0 )

                notes.nextPage( this );

            else
                latch.countDown();

           //////////////////////////////////

         /// do whatever I want with the fetched data


 }

        @Override
        public void handleFault(BackendlessFault fault) {// here we have the error  of request 

            swipeToReload.setRefreshing(false);
            Toast.makeText(getContext(), "" + fault.getMessage(), Toast.LENGTH_LONG).show();

        }
    });
    }

so now the above function is fetching 10 items per request in a loop and this loop runs until all the data is retrieved, what I want is to make this loop run manually (on a button click I want to load the first 10 items, then instead of requesting again I want it to work manually, fetch the next 10 items on button click) if anybody can guide me to right direction then it'll be so helpful for me


回答1:


Remove countdown latch and change the signature like this:

public void fetchData( int offset )

In the implementation, keep the code to set the page size, but also add this:

dataQuery.setOffset( offset );

That will fetch the next page of data from the specified offset. There is no need to call nextPage in the responder. The result you get will be the block of "pageSize" records from the specified offset.



来源:https://stackoverflow.com/questions/38250686/control-an-automated-function-manually

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!