Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

文件加载时,执行FileFilter.getFiles 会造成主线程阻塞,如果放在子线程。又会崩溃 #20

Open
ychdevelop opened this issue Nov 1, 2017 · 2 comments

Comments

@ychdevelop
Copy link

rt

@Comert007
Copy link

Comert007 commented Nov 28, 2018

在子线程前面加上:
Looper.prepare();

完整代码:

   private void loadData() {
    
    Disposable disposable = Observable.create(new ObservableOnSubscribe<List<Directory<NormalFile>>>() {
        @Override
        public void subscribe(ObservableEmitter<List<Directory<NormalFile>>> emitter) throws
                Exception {
            Looper.prepare();

            FileFilter.getFiles(NormalFilePickActivity.this, new FilterResultCallback<NormalFile>() {
                @Override
                public void onResult(List<Directory<NormalFile>> directories) {
                    // Refresh folder list
                   emitter.onNext(directories);
                   emitter.onComplete();
                }
            }, mSuffix);

        }
    }).subscribeOn(Schedulers.io())//异步任务在IO线程执行
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<List<Directory<NormalFile>>>() {
                @Override
                public void accept(List<Directory<NormalFile>> directories) throws Exception {
                    if (isNeedFolderList) {
                        ArrayList<Directory> list = new ArrayList<>();
                        Directory all = new Directory();
                        all.setName(getResources().getString(R.string.vw_all));
                        list.add(all);
                        list.addAll(directories);
                        mFolderHelper.fillData(list);
                    }

                    mAll = directories;
                    refreshData(directories);
                }
            });
}

@shengguan
Copy link

在子线程前面加上:
Looper.prepare();

完整代码:

   private void loadData() {
    
    Disposable disposable = Observable.create(new ObservableOnSubscribe<List<Directory<NormalFile>>>() {
        @Override
        public void subscribe(ObservableEmitter<List<Directory<NormalFile>>> emitter) throws
                Exception {
            Looper.prepare();

            FileFilter.getFiles(NormalFilePickActivity.this, new FilterResultCallback<NormalFile>() {
                @Override
                public void onResult(List<Directory<NormalFile>> directories) {
                    // Refresh folder list
                   emitter.onNext(directories);
                   emitter.onComplete();
                }
            }, mSuffix);

        }
    }).subscribeOn(Schedulers.io())//异步任务在IO线程执行
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Consumer<List<Directory<NormalFile>>>() {
                @Override
                public void accept(List<Directory<NormalFile>> directories) throws Exception {
                    if (isNeedFolderList) {
                        ArrayList<Directory> list = new ArrayList<>();
                        Directory all = new Directory();
                        all.setName(getResources().getString(R.string.vw_all));
                        list.add(all);
                        list.addAll(directories);
                        mFolderHelper.fillData(list);
                    }

                    mAll = directories;
                    refreshData(directories);
                }
            });
}

报错了
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants