Java 如何停止应用程序,直到可观察到的线程结束?
我正在使用改型和RxJava2。在我的应用程序中,我有一个名为Java 如何停止应用程序,直到可观察到的线程结束?,java,android,observable,retrofit,rx-java2,Java,Android,Observable,Retrofit,Rx Java2,我正在使用改型和RxJava2。在我的应用程序中,我有一个名为apiServiceGroups的可观察的。据我所知,Observable在另一个线程中运行,而应用程序继续在主线程中工作 我希望应用程序的主线程等到这部分代码运行完毕。我怎么做 我的代码: disposable=apiServiceGroups.getGroupsList(String.format(“{\”offset\”:%s}”,groupCount)) .subscribeOn(Schedulers.io()) .obser
apiServiceGroups
的可观察的
。据我所知,Observable
在另一个线程中运行,而应用程序继续在主线程中工作
我希望应用程序的主线程等到这部分代码运行完毕。我怎么做
我的代码:
disposable=apiServiceGroups.getGroupsList(String.format(“{\”offset\”:%s}”,groupCount))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅(新消费者){
@凌驾
public void accept(GroupsListResponse GroupsListResponse)引发异常{
对于(int i=0;i
在accept方法中完成API任务之前,必须隐藏所有视图/片段并显示加载指示器或进度条
一旦完成并得到响应,就必须隐藏加载指示器并显示视图/片段
API调用
public LiveData<Boolean> apiCall() {
MutableLiveData<Boolean> loading = new MutableLiveData();
Disposable disposable = apiServiceGroups.getGroupsList(String.format("{\"offset\":%s}", groupCount))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<GroupsListResponse>() {
@Override
public void accept(GroupsListResponse groupsListResponse) throws Exception {
for (int i = 0; i < groupsListResponse.getGroups().size(); i++) {
String currentName = groupsListResponse.getGroups().get(i).getGroupFullName();
if (groupName.equals(currentName.split(" ")[0])) {
familiar.add(currentName);
}
}
loading.postValue(true);
}
, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Log.e("thro", Objects.requireNonNull(throwable.getMessage()));
}
}
});
return loading;
}
很抱歉,我仍然不知道如何连接进度条和正在运行的进程。我应该如何停止应用程序并启动进度条?
final Observer<String> nameObserver = new Observer<String>() {
@Override
public void onChanged(@Nullable final String newName) {
// Update the UI
}
};
apiCall().observe(this, nameObserver);
interface Response {
public void onSuccess();
public void onFailure();
}
if (groupName.equals(currentName.split(" ")[0])) {
familiar.add(currentName);
}
}
callback.success()
}