0

When I'm pressing on the item in my navigationView I call getSupportFM and load fragment. In the fragment I attach ViewModel and in the constructor and then I load my data from network and in this time in the method .doOnNext save it's in Realm database.

public MyViewModel() {
    repository = new MyRepository();
    realm = Realm.getDefaultInstance();
    liveData = new LiveRealmResults<>(realm.where(MyClass.class).findAllAsync());
}

My network request:

apiInterface.getMyData(page)
            .subscribeOn(Schedulers.io())
            .doOnNext(myDataRequest -> {
                if (myDataRequest.getMyData().size() != 0) {
                    List<MyData> myData= myDataRequest.getMyData();
                    try (Realm realm = Realm.getDefaultInstance()) {
                         realm.executeTransaction(r -> r.insertOrUpdate(myData));
                    }
                }
            })
            .observeOn(AndroidSchedulers.mainThread())

My fragment:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //we should to do this.
    //https://developer.android.com/jetpack/arch/livedata#the_advantages_of_using_livedata
    viewModel = ViewModelProviders.of(getActivity()).get(MyViewModel.class);
    viewModel.getMyLiveData().observe(this, stateObserver);
}

And Observer:

private Observer<List<MyData>> stateObserver = new Observer<List<MyData>>() {
    @Override
    public void onChanged(@Nullable List<MyData> myData) {
        if (myData.size() != 0) {
            progressBar.setVisibility(View.GONE);
            adapter.updateAdapter(myData);
        }
    }
};

But, if I deleting this line DrawerLayout isn't laggy when I'm swapping between fragments with navigationView. In the first time, I load 20 elements from network.

I've spent two days to fix it but I don't understand how to do this.

viewModel.getMyLiveData().observe(this, stateObserver);
2
  • Nothing seems out of the ordinary, you'll need to use method profiling and post here what seems slowest. Jul 6, 2018 at 8:55
  • Thanks for the reply. I'll try and post it in 3-5 hours
    – NyanNyan
    Jul 6, 2018 at 10:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.