好的,所以我在我的Android应用程序中使用的RxJava Observable遇到了一些麻烦。这是非常令人沮丧的,因为这已经工作了一段时间,现在只是抛出错误。我知道这意味着我正在从另一个线程进行UI操作,但我不知道发生了什么。所以这是可观察的:
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
}
});
经过一些调试后,我发现这个平面图是发生错误的地方:
.flatMap(s -> mainData.getDataFromNetwork(this, mCurrentUser, mSimpleFacebook))
其中包括以下内容,我添加了一个注释错误的注释:
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");
}
}
这是怎么回事?它一直工作得很好,现在它说我在一些帮助线程。就我而言,到目前为止,我一直在UI线程上。如果有人可以帮助我,这将是伟大的,否则我可能不得不放弃RxJava暂时因为它不适合我。提前谢谢!