1

I'm trying to build an app to fetch list of feed from server and display in Recyclerview. I am trying out basic implementation of LiveData like this.

I have set up an observer in my Fragment as follows:

viewModel = ViewModelProviders.of(getActivity()).get(SellViewModel.class);

    viewModel.getSellItemList(19).observe(this, new Observer<List<LambdaSellRequestClass>>() {
            @Override
            public void onChanged(@Nullable List<LambdaSellRequestClass> sellItems) {


                adapter.setSellEntities(sellItems);

            }
        });

My SellViewModel clas like this:

public class SellViewModel extends AndroidViewModel {

private SellRepository repository;

private MutableLiveData<List<LambdaSellRequestClass>> sellItems;

public SellViewModel(@NonNull Application application) {
    super(application);
    repository = new SellRepository(application);

    try {
        if (sellItems == null) {
            sellItems = new MutableLiveData<>();
            sellItems.postValue(repository.getSellItemList(user_id));
        }
    }catch (Exception e) {
        Log.d("SELLFRAGMENT", "Error: " + e.getLocalizedMessage());
    }
}

public MutableLiveData<List<LambdaSellRequestClass>> getSellItemList(int userId) throws ExecutionException, InterruptedException {

    return sellItems;
}

}

My SellRepository like this:

public class SellRepository {

public SellRepository(Application application) {

}

    public List<LambdaSellRequestClass> getSellItemList(int userId) throws ExecutionException, InterruptedException {

        return new SellRepository.GetSellItemListAsync(SellRepository.this).execute(userId).get();

    }

   private static class GetSellItemListAsync extends AsyncTask<Integer, Void, List<LambdaSellRequestClass>> {

        List<LambdaSellRequestClass> list = new ArrayList<>();      

        public GetSellItemListAsync() {

        }

        @Override
        protected List<LambdaSellRequestClass> doInBackground(Integer... integers) {

            final int userID = integers[0];


          list =     

     lambdaFunctionsCalls.getSellItemByUser_lambda(requestClass).getSellItems();

            return list;
        }
  }

My problem is when I add new sell items to database its not update mobile app.

stay_hungry
  • 1,448
  • 1
  • 14
  • 21
  • Do you mean the app will update sell data automatically without reopening or refresh? – aiueoH May 03 '19 at 01:09
  • The way you use `AsyncTask` seems a bit off. Are you sure your server request is running asynchronously? Also, show us `lambdaFunctionsCalls.getSellItemByUser_lambda(requestClass).getSellItems()` implementation too. – Sanlok Lee May 03 '19 at 01:24
  • @aiueoH Yes. When I add sell item to database in my server, the client will update real time – stay_hungry May 03 '19 at 03:23
  • @SanlokLee Yes. Request is running asynchronously. lambdaFunctionsCalls.getSellItemByUser_lambda(requestClass).getSellItems() is server side method. Its AWS Lambda function. – stay_hungry May 03 '19 at 03:25
  • @stay_hungry LiveData cannot fetch data from server automatically, you need notify app by Push or others way like Firebase realtime database. – aiueoH May 24 '19 at 09:07

0 Answers0