Ric*_*ick 5 java multithreading android rx-java rx-android
好的,所以我在我的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
}
});
Run Code Online (Sandbox Code Playgroud)
经过一些调试后,我发现这个flatmap是发生错误的地方:
.flatMap(s -> mainData.getDataFromNetwork(this, mCurrentUser, mSimpleFacebook))
Run Code Online (Sandbox Code Playgroud)
其中包括以下内容,我添加了一个注释错误的注释:
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");
}
}
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?它一直工作得很好,现在它说我在一些帮助线程.就我而言,到目前为止,我一直在UI线程上.如果有人可以帮助我,这将是伟大的,否则我可能不得不放弃RxJava暂时因为它不适合我.提前致谢!
看起来该simpleFacebook.getProfile(properties, listener)调用在内部有自己的线程。通常您不希望 rxjava 这样做。您希望 rxjava 处理线程,并且您编写的所有代码都应该是同步的。
我不熟悉 Facebook SDK,但我会寻找同步执行请求的调用。因此,一种看起来像public Profile getProfile(Properties properties) {而不是返回void并需要侦听器的方法。
希望这可以帮助..
| 归档时间: |
|
| 查看次数: |
1935 次 |
| 最近记录: |