jeudi 13 août 2015

RxJava Can't create handler inside thread that has not called Looper.prepare()

Okay, so I am having a bit of trouble with an RxJava Observable I am using in my Android app. It is extremely frustrating because this has been working for a while now and is only now throwing the error above. I am aware that this means I am doing a UI operation from another thread but I do not see where that is happening. So here is the observable:

 ConnectionsList.getInstance(this).getConnectionsFromParse(mCurrentUser)
            .delay(3, TimeUnit.SECONDS)
            .flatMap(s -> mainData.getDataFromNetwork(this, mCurrentUser, mSimpleFacebook))
            .flatMap(s -> mainData.getPictureFromUrl(s))
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Observer<Bitmap>() {
                @Override
                public void onCompleted() {
                    if (mCurrentUser.get(Constants.NAME) != null) {
                        mNavNameField.setText((String) mCurrentUser.get(Constants.NAME));
                    }
                    mSearchFragment.setupActivity();
                }

                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    mSearchFragment.setEmptyView();
                }

                @Override
                public void onNext(Bitmap bitmap) {
                    mNavProfImageField.setImageBitmap(bitmap);
                    mainData.saveImageToParse(bitmap); //save the image to Parse backend
                }
            });

After some debugging I found that this flatmap is where the error is happening:

.flatMap(s -> mainData.getDataFromNetwork(this, mCurrentUser, mSimpleFacebook))

Inside of that is the following, I have added a comment where the error is being thrown:

 return Observable.create(subscriber -> {
            //set the username field if ParseUser is not null
            if(currentUser != null) {
                username = (String) currentUser.get(Constants.NAME);
            }

            //if prof pic is null then request from facebook. Should only be on the first login
            if (currentUser != null && currentUser.getParseFile(Constants.PROFILE_IMAGE) == null) {
                if (AccessToken.getCurrentAccessToken() != null) { //check if session opened properly
                    //get simple facebook and add the user properties we are looking to retrieve
                    Profile.Properties properties = new Profile.Properties.Builder()
                            .add(Profile.Properties.FIRST_NAME)
                            .add(Profile.Properties.GENDER)
                            .add(Profile.Properties.BIRTHDAY)
                            .add(Profile.Properties.ID)
                            .add(Profile.Properties.EMAIL)
                            .build();

                    //The following line is where the debugger stops 
                    //and the error gets thrown
                    simpleFacebook.getProfile(properties, new OnProfileListener() {
                        @Override
                        public void onComplete(Profile response) {
                            String id = response.getId();
                            String name = response.getFirstName();
                            String gender = response.getGender();
                            String birthday = response.getBirthday();
                            String email = response.getEmail();
                            String age = getAge(birthday);

                            currentUser.put(Constants.NAME, name);
                            currentUser.put(Constants.AGE, age);
                            currentUser.put(Constants.GENDER, gender);
                            currentUser.put(Constants.EMAIL, email);
                            currentUser.saveInBackground();

                            if (id != null) { //display the profile image from facebook
                                subscriber.onNext("http://ift.tt/wljqS4" + id + "/picture?type=large");
                            }
                        }

Whats going on here? Its been working fine as is and now it is saying that I am on some helper thread. As far as I was concerned I was on the UI thread up to this point. If someone can help me that would be great, otherwise I may have to drop RxJava for the time being as it is not working out for me. Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire