Ok, so I have problems with the RxJava Observable, which I use in my Android application. This is very unpleasant, because it has been working for a long time and is now throwing the error above. I know this means that I am performing a user interface operation from another thread, but I cannot see where this 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);
After some debugging, I found that this flat card is causing the error:
.flatMap(s -> mainData.getDataFromNetwork(this, mCurrentUser, mSimpleFacebook))
Inside this there is the following: I added a comment where an error occurs:
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("https://graph.facebook.com/" + id + "/picture?type=large"); } }
What's going on here? It works fine as it is, and now it says I'm in some sort of supporting topic. As far as I know, so far I have been on the topic of user interface. If someone can help me, that would be great, otherwise I might have to give up RxJava, as that does not work for me. Thanks in advance!