2

How to use Saved State module for ViewModel in Background Thread

For MutableLiveData we have the option to use setvalue and postvalue , where Postvalue can be used in background thread.

How ever How can we use BACKGROUND THREAD FOR Saved State module for ViewModel

here Is the code I am trying

public class CommonViewModel extends ViewModel {
    private SavedStateHandle mState;

    public CommonViewModel(SavedStateHandle savedStateHandle) {
        mState = savedStateHandle;
    }

    private static final String NAME_KEY = "name";
    private Executor mExecutor = Executors.newSingleThreadExecutor();

    public LiveData<ArrayList<CommonOwn>> getCart() {
        if (mState.getLiveData(NAME_KEY) == null) {
            initCart();
        }
        return mState.getLiveData(NAME_KEY);
    }

    public void initCart() {
        mState.set(NAME_KEY, new ArrayList<CommonOwn>());
    }

  public void addItemToCart(CommonOwn commonOwn) {
        if (getCart().getValue() == null) {
            initCart();
        }
        ArrayList<CommonOwn> cartItemList = new ArrayList<CommonOwn>(getCart().getValue());
        if (cartItemList.contains(commonOwn)) {
            int a = cartItemList.indexOf(commonOwn);
            cartItemList.remove(a);
        } else {
            cartItemList.add(commonOwn);
        }
       // mState.set(NAME_KEY, cartItemList);
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                mState.set(NAME_KEY, cartItemList);

            }
        });
    }
}

when using background thread The following error occurs

java.lang.IllegalStateException: Cannot invoke setValue on a background thread
        at androidx.lifecycle.LiveData.assertMainThread(LiveData.java:487)
        at androidx.lifecycle.LiveData.setValue(LiveData.java:306)
        at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
        at androidx.lifecycle.SavedStateHandle$SavingStateLiveData.setValue(SavedStateHandle.java:367)
        at androidx.lifecycle.SavedStateHandle.set(SavedStateHandle.java:256)
        at com.example.CommonViewModel$1.run(CommonViewModel.java:63)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)

how can we solve this issue.

smitop
  • 4,770
  • 2
  • 20
  • 53
1234567
  • 2,226
  • 4
  • 24
  • 69

1 Answers1

1

The following line can never be null:

mState.getLiveData(NAME_KEY) == null

Hope this illustrates the solution. You just rely on the MutableLiveData from SavedStateHandle:

public class CommonViewModel extends ViewModel {
    private MutableLiveData<ArrayList<CommonOwn>> cart;

    public CommonViewModel(SavedStateHandle savedStateHandle) {
        cart = savedStateHandle.getLiveData(NAME_KEY);
    }

    private static final String NAME_KEY = "name";
    private Executor mExecutor = Executors.newSingleThreadExecutor();

    public MutableLiveData<ArrayList<CommonOwn>> getCart() {
        return cart;
    }

    public void addItemToCart(CommonOwn commonOwn) {
        ArrayList<CommonOwn> cartItemList;
        if(cart.getValue() == null) {
            cartItemList = new ArrayList<CommonOwn>();
        } else {
            cartItemList = cart.getValue();
        }
        if (cartItemList.contains(commonOwn)) {
            int a = cartItemList.indexOf(commonOwn);
            cartItemList.remove(a);
        } else {
            cartItemList.add(commonOwn);
        }
        // mState.set(NAME_KEY, cartItemList);
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                cart.postValue(cartItemList);
            }
        });
    }
}
drlue
  • 1,143
  • 9
  • 8