无法从片段中的ViewModel观察LiveData

时间:2019-09-06 21:08:33

标签: android android-livedata android-viewmodel mutablelivedata

我在RecyclerView之一的ViewPager中有一个Fragment。此RecyclerView节目的数据是从服务器获取的。它通常可以正常工作,但是我无法使用ViewModelLiveData来实现相同的功能,因为在livedata.observe被调用时livedata方法没有被调用从ViewModel更改。

这是我的 MonthlyOrderViewModel.java 类的源代码

public class MonthlyOrderViewModel extends AndroidViewModel {

private Retrofit retrofit;
private RetrofitServices retrofitServices;
private MutableLiveData<MonthlyOrderHistory> monthlyOrderHistoryMutableLiveData = new MutableLiveData<>();

public MonthlyOrderViewModel(@NonNull Application application) {
    super(application);
    retrofit = RetrofitFactory.getRetrofit();
    retrofitServices = retrofit.create(RetrofitServices.class);
}

public MutableLiveData<MonthlyOrderHistory> getMonthlyOrderHistoryMutableLiveData() {
    return monthlyOrderHistoryMutableLiveData;
}
public void fetchMonthlyOrders() {
    Call<MonthlyOrderHistory> call = retrofitServices.getAllMonthlyOrderHistory(IpharmaApplication.getInstance().getLoggedInUser().getId());
    call.enqueue(new Callback<MonthlyOrderHistory>() {
        @Override
        public void onResponse(Call<MonthlyOrderHistory> call, Response<MonthlyOrderHistory> response) {

            MonthlyOrderHistory monthlyOrderHistory = response.body();
            if(monthlyOrderHistory.getStatus().equalsIgnoreCase("success")) {

                List<UserOrder> userOrders = monthlyOrderHistory.getOrders().getUserOrder();
                if(userOrders != null && userOrders.size() != 0) {
                    DefaultScheduler.INSTANCE.execute(() -> {
                        monthlyOrderHistoryMutableLiveData.postValue(monthlyOrderHistory);
                        return Unit.INSTANCE;
                    });
                }
            }
        }

        @Override
        public void onFailure(Call<MonthlyOrderHistory> call, Throwable t) {

            t.printStackTrace();
        }
    });
}

}

DefaultSchedulerScheduler,默认情况下处理AsyncScheduler中的操作。 这是我的 Scheduler.kt

的源代码

interface Scheduler {

    fun execute(task: () -> Unit)

    fun postToMainThread(task: () -> Unit)

    fun postDelayedToMainThread(delay: Long, task: () -> Unit)
}

//A shim [Scheduler] that by default handles operations in the [AsyncScheduler].
object DefaultScheduler : Scheduler {

    private var delegate: Scheduler = AsyncScheduler

    //Sets the new delegate scheduler, null to revert to the default async one.
    fun setDelegate(newDelegate: Scheduler?) {
        delegate = newDelegate ?: AsyncScheduler
    }

    override fun execute(task: () -> Unit) {
        delegate.execute(task)
    }

    override fun postToMainThread(task: () -> Unit) {
        delegate.postToMainThread(task)
    }

    override fun postDelayedToMainThread(delay: Long, task: () -> Unit) {
        delegate.postDelayedToMainThread(delay, task)
    }
}

//Runs tasks in a [ExecutorService] with a fixed thread of pools
internal object AsyncScheduler : Scheduler {

    private val executorService: ExecutorService = Executors.newFixedThreadPool(NUMBER_OF_THREADS)

    override fun execute(task: () -> Unit) {
        executorService.execute(task)
    }

    override fun postToMainThread(task: () -> Unit) {
        if (isMainThread()) {
            task()
        } else {
            val mainThreadHandler = Handler(Looper.getMainLooper())
            mainThreadHandler.post(task)
        }
    }

    private fun isMainThread(): Boolean {
        return Looper.getMainLooper().thread === Thread.currentThread()
    }

    override fun postDelayedToMainThread(delay: Long, task: () -> Unit) {
        val mainThreadHandler = Handler(Looper.getMainLooper())
        mainThreadHandler.postDelayed(task, delay)
    }
}

//Runs tasks synchronously.
object SyncScheduler : Scheduler {
    private val postDelayedTasks = mutableListOf<() -> Unit>()

    override fun execute(task: () -> Unit) {
        task()
    }
    override fun postToMainThread(task: () -> Unit) {
        task()
    }
    override fun postDelayedToMainThread(delay: Long, task: () -> Unit) {
        postDelayedTasks.add(task)
    }
    fun runAllScheduledPostDelayedTasks() {
        val tasks = postDelayedTasks.toList()
        clearScheduledPostdelayedTasks()
        for (task in tasks) {
            task()
        }
    }
    fun clearScheduledPostdelayedTasks() {
        postDelayedTasks.clear()
    }
}

最后,我尝试使用以下方法观察片段的onCreateView方法的变化:

MonthlyOrderViewModel monthlyOrderViewModel = new MonthlyOrderViewModel(getActivity().getApplication());

monthlyOrderViewModel.getMonthlyOrderHistoryMutableLiveData().observe(this, monthlyOrderHistory -> {

    monthlyOrderRecyclerView = view.findViewById(R.id.monthlyOrderRecyclerView);

    monthlyOrderRecyclerViewAdapter = new OrderHistoryRecyclerViewAdapter(getContext(), monthlyOrderHistory.getOrders().getUserOrder());

    monthlyOrderRecyclerViewAdapter.notifyDataSetChanged();

    monthlyOrderRecyclerView.setAdapter(monthlyOrderRecyclerViewAdapter);       

    monthlyOrderRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

});

我尝试对其进行调试,发现改装在获取数据方面没有问题。但是当我尝试在片段中使用它们时,observe方法永远不会被调用。

请建议我如何解决此问题?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

您不应使用ViewModel实例化new。您必须改为:

MonthlyOrderViewModel monthlyOrderViewModel = ViewModelProviders.of(this).get(MonthlyOrderViewModel.class);

如果您确定postValue在ViewModel中正在执行,那么我认为此更改应使其起作用。还有一件事:观察到的LiveData每次通知您更改时,您都不应该那样做。我会将下一行移到observe()

之前
monthlyOrderRecyclerView = view.findViewById(R.id.monthlyOrderRecyclerView);

monthlyOrderRecyclerViewAdapter = new OrderHistoryRecyclerViewAdapter(getContext(), monthlyOrderHistory.getOrders().getUserOrder());

monthlyOrderRecyclerView.setAdapter(monthlyOrderRecyclerViewAdapter);       

monthlyOrderRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

更新

正如Fahim在评论中确认的那样,问题是正在创建同一ViewModel的多个实例。如果需要共享ViewModel的相同实例,请确保传递作为该ViewModel的“所有者”的Fragment / Activity的引用。例如,如果MyActivity有一个MainActivityViewModel并且它包含一个名为MyFragment的子Fragment,那么如果该Fragment想要获取对其父级ViewModel的引用,则必须在通过以下方式:

MainActivityViewModel mainActivityVM = ViewModelProviders
                                            .of(getActivity())
                                            .get(MainActivityViewModel.class);