-1
observer = new Observer<Resource<List<DateProfile>>>() {
        @Override
        public void onChanged(@Nullable Resource<List<DateProfile>> resource) {
            if (resource.mData != null) {
                Log.d("vcvcv", resource.mStatus + "   " + resource.mMessage + "  " + resource.mCode + "  " + resource.mData.size());
                List<DateProfile> users = resource.mData;
                if (users != null) {
                    dateProfileList.clear();
                    dateProfileList.addAll(users);
                    homeDatingAdapter.notifyDataSetChanged();

                    homeDatingViewModel.getHomeDateProfiles().removeObserver(observer);
                    homeDatingViewModel.getHomeDateProfiles().removeObserver(this);
                }
            } else {
                Log.d("users", "resource is null");
            }
        }
    };

    homeDatingViewModel.getHomeDateProfiles().observe(this, observer);

this observers calling in fragment to get data from local db(Room) and i am getting last live data from room db.

This is my Dao interface

@Query("SELECT * FROM DateProfile")
LiveData<List<DateProfile>> getHomeDateProfile();

when i am deleting any row from room db, its observing the viewmodel.

int isDeleted = delHomeDatingModel.deleteDateProfileFromDb(dateProfileList.get(currentItem).getUserid());

This is my ViewModel class

public class HomeDatingViewModel extends ViewModel {
private final HomeDateProfileReposotories homeDateProfileReposotories;

@Inject
public HomeDatingViewModel(HomeDateProfileReposotories homeDateProfileReposotories) {
    this.homeDateProfileReposotories = homeDateProfileReposotories;
}

public LiveData<Resource<List<DateProfile>>> getHomeDateProfiles() {
    return homeDateProfileReposotories.getDateProfiles();
}

public LiveData<List<DateProfile>> getDateProfileFromDb() {
    return homeDateProfileReposotories.getDateProfilesFromDb();
}

public int deleteDateProfileFromDb(String userId) {
    return homeDateProfileReposotories.deleteDateProfilesFromDb(userId);
}}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.