[英]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. 好的,所以我在我的Android应用程序中使用的RxJava Observable遇到了一些麻烦。 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.
我知道这意味着我正在从另一个线程进行UI操作,但我不知道发生了什么。 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是发生错误的地方:
.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("https://graph.facebook.com/" + 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.
就我而言,到目前为止,我一直在UI线程上。 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.
如果有人可以帮助我,这将是伟大的,否则我可能不得不放弃RxJava暂时因为它不适合我。 Thanks in advance!
提前致谢!
It looks like the simpleFacebook.getProfile(properties, listener)
call does it's own threading internally. 看起来
simpleFacebook.getProfile(properties, listener)
调用在内部执行它自己的线程。 Typically you don't want this with rxjava. 通常,您不希望使用rxjava。 You want rxjava to take care of the threading and all code you write should be synchronous.
您希望rxjava处理线程,并且您编写的所有代码都应该是同步的。
I am not familiar with the Facebook SDK, but I would look for a call that executes the request synchronously. 我不熟悉Facebook SDK,但我会寻找一个同步执行请求的调用。 So a method that looks like
public Profile getProfile(Properties properties) {
instead of one returning void
and requiring a listener. 所以这个方法看起来像
public Profile getProfile(Properties properties) {
而不是一个返回void
并需要一个监听器。
Hope this helps.. 希望这可以帮助..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.