rxjava-在viewmodel中,我必须在哪里为方法编写线程?

v9tzhpje  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(461)

我有:在道:

@Query("SELECT * FROM person_table WHERE status = :status_mudak ORDER BY RANDOM() LIMIT 5")
    Single<List<Person>> getFivePersonsFrom(String status_mudak);

回购:

public class PersonRepository {
    public Single<List<Person>> getFivePersonsFrom(String status_mudak) {
        return mPersonDao.getFivePersonsFrom(status_mudak);
    }
}

在viewmodel中:

public class PersonViewModel extends AndroidViewModel {
    private PersonRepository mRepository;
    //declaring variables
    public PersonViewModel(@NonNull Application application) {
        super(application);
        mRepository = new PersonRepository(application);
        //initializing variables
    }
    //methods
}

在viewmodel中,我必须分配线程,以便将方法进一步移交给livedata?

9cbw7uwe

9cbw7uwe1#

public class PersonViewModel extends AndroidViewModel {
private PersonRepository mRepository;
private MutableLiveData<List<Person>> mPersonList = MutableLiveData<List<Person>>();
//declaring variables
public PersonViewModel(@NonNull Application application) {
    super(application);
    mRepository = new PersonRepository(application);
    //initializing variables
}

LiveData<List<Person>> getPersonList() {
    return mPersonList;
}

private void extractPersonList() {
    mRepository.getFivePersonsFrom("some_mudatskiy_status")
        .observeOn(/*rxSchedulers.main*/)
        .subscribe(this::updatePersonList);
}

private void updatePersonList(List<Person> personList) {
    mPersonList.postValue(personList);
}
//methods

}

相关问题