2015-08-14 56 views
5

好的,所以我在我的Android应用程序中使用RxJava Observable时遇到了一些问题。这是非常令人沮丧的,因为这已经工作了一段时间,现在只是抛出上述错误。我知道这意味着我正在从另一个线程进行UI操作,但我不知道发生了什么。因此,这里是观察到:RxJava无法在未调用Looper.prepare()的线程中创建处理程序()

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是在错误发生的事情:

.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,因为它不适合我。提前致谢!

+0

我怀疑问题是这样的:'.subscribeOn(Schedulers.io())'。您应该在主线程调度程序上订阅,因为您的网络操作似乎是异步的。 – corsair992

+0

你可以发布堆栈跟踪吗?是否因为'getProfile'需要在主线程中调用? – zsxwing

+0

我怀疑getprofile需要在主线程中调用,但我不会将它混淆为什么它在以前工作。我应该有subscribeOn(AndroidSchedulers.MainThread)吗? –

回答

0

它看起来像simpleFacebook.getProfile(properties, listener)调用它自己的内部线程。通常情况下,你不需要rxjava。您希望rxjava处理线程,并且您编写的所有代码都应该是同步的。

我不熟悉Facebook SDK,但我会寻找一个同步执行请求的调用。因此,看起来像public Profile getProfile(Properties properties) {而不是返回void并需要侦听器的方法。

希望这有助于..

相关问题