RXJava和DialogFragment => Dialog等待观察 - 为什么?

时间:2016-01-26 09:00:18

标签: android rx-java android-dialogfragment

我显示DialogFragment并在后台加载数据。我希望对话框显示,数据继续加载并逐步更新已经可见的对话框。

问题

在observable完成之前,不会显示该对话框。为什么呢?

代码

private final static Handler mHandler = new Handler();

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    Observable<List<Data>> observable =  Observable.create(new Observable.OnSubscribe<Data>()
    {
        @Override
        public void call(Subscriber<? super Data> subscriber)
        {

            // iterate and create data
            for (int i = resolveInfo.size() - 1; i >= 0; i--)
            {
                 Data d = ...;
                 subscriber.onNext(d);
            }

            subscriber.onCompleted();
        }
    })
            .onBackpressureBuffer()
            .buffer(150, TimeUnit.MILLISECONDS)
            .filter(new Func1<List<Data>, Boolean>()
            {
                @Override
                public Boolean call(List<Data> rxSingleDatas)
                {
                    return rxSingleDatas.size() > 0; // only deliver not emtpy lists!
                }
            })
            .onBackpressureBuffer()
            .doOnNext(new Action1<List<Data>>()
            {
                @Override
                public void call(List<Data> shareDatas)
                {
                    // add result to list + notify adapter
                    for (int i = 0; i < shareDatas.size(); i++)
                        mData.add(shareData);
                    if (mAdapter != null)
                        mAdapter.notifyDataSetChanged();
                }
            })
            .doOnCompleted(new Action0()
            {
                @Override
                public void call()
                {
                    // flag to indicate all data is loaded
                    mReady = true;
                }
            });

    // subscribe on handler thread for async execution
    // observe on main thread for updating UI
    mSubscription = observable
            .subscribeOn(HandlerScheduler.from(mHandler))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe();
}

@Override
public void onDestroy()
{
    if (mSubscription != null)
        mSubscription.unsubscribe();
    super.onDestroy();
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
    MaterialDialog.Builder builder = new MaterialDialog.Builder(getActivity())
            .title(R.string.share_with)
        .cancelable(true)
        .autoDismiss(false);

    mAdapter = ...;
    builder
            .adapter(mAdapter, new MaterialDialog.ListCallback() {
                @Override
                public void onSelection(MaterialDialog materialDialog, View view, int i, CharSequence charSequence) {

                    if (!mReady)
                        return;

                    // handle click
                }
            });

    MaterialDialog dlg = builder.build();
    return dlg;
}

修改

我用后台线程上运行的处理程序替换了处理程序,但这会导致以下执行:CalledFromWrongThreadException尝试在doOnNext调用中更新适配器时... {I会说observeOn应该具有在主线程上调用doOnNext的效果,但似乎这不起作用......

1 个答案:

答案 0 :(得分:1)

subscribeOnobserveOn运算符的工作方式存在一些混淆。

subscribeOn()将线程应用于生成部分(此处为Observable.create())以及之后的任何内容,直到第一个observeOn()或运算符在特定线程上进行设计。因此,observeOn切换线程以便链中的下游操作。 RxJava不会自动切换线程,直到明确告知。