【问题标题】:How to use RxJava instead of LiveData with android architecture components?如何在 android 架构组件中使用 RxJava 而不是 LiveData?
【发布时间】:2018-10-21 17:28:59
【问题描述】:

注意:如果您已经在使用 RxJava 或 Agera 等库,您可以 继续使用它们而不是 LiveData。但是当你使用它们或 其他方法,确保您正确处理生命周期 这样当相关的 LifecycleOwner 出现时,您的数据流就会暂停 停止并且当 LifecycleOwner 被销毁时流被销毁 被摧毁。您还可以添加 android.arch.lifecycle:reactivestreams 将 LiveData 与另一个反应流库一起使用的工件(对于 例如,RxJava2)。

上面的语句是从android开发者页面复制的。这里它指定如果你使用RxJava你不需要使用LiveData。因为两者都遵循Observable Patters。我想知道如何使用RxJava而不是LiveData和rest api调用。我尝试了很多,但找不到任何答案。如果有人帮助我解决这个问题,那就太好了。提前谢谢

【问题讨论】:

    标签: android rx-android android-architecture-components


    【解决方案1】:

    您可以使用这些方法将Publisher<T> 转换为LiveData<T>,反之亦然。

    • LiveDataReactiveStreams.fromPublisher(Publisher<T> publisher) -- 会给你LiveData对象

    • LiveDataReactiveStreams.toPublisher(LifecycleOwner lifecycle, LiveData<T> liveData) -- 会给你的RxJava Publisher<T> 对象..

    然后你可以使用pub-sub 模式和subscribe to the publisher

    【讨论】:

    • 谢谢。我会调查这个
    【解决方案2】:

    看看

    https://developer.android.com/reference/android/arch/lifecycle/LiveDataReactiveStreams

    RxJava 的 Flowable 实现了Publisher 接口。您可以将LiveData 转换为Flowable 并从那里开始。

    【讨论】:

      【解决方案3】:

      这是一个简单的例子,使用 RX 的改造来解析 GET 响应

      假设响应

         {
                  "id": "0001",
                      "type": "donut",
                      "name": "Cake",
                      "image": {
                  "url": "images/0001.jpg",
                          "width": 200,
                          "height": 200
              },
                  "thumbnail": {
                  "url": "images/thumbnails/0001.jpg",
                          "width": 32,
                          "height": 32
              }
      

      API接口

       @GET("nestedjson5")
          Observable<NestedJson5Main> nestedJson5();
      

      图片

      public class Image {
      
          String url;
          int width;
          int height;
      
          public String getUrl() {
              return url;
          }
      
          public void setUrl(String url) {
              this.url = url;
          }
      
          public int getWidth() {
              return width;
          }
      
          public void setWidth(int width) {
              this.width = width;
          }
      
          public int getHeight() {
              return height;
          }
      
          public void setHeight(int height) {
              this.height = height;
          }
      }
      

      模型类

      public class NestedJson5Main {
      
          String id;
          String type;
          String name;
          Image image;
          Image thumbnail;
      
          public String getId() {
              return id;
          }
      
          public void setId(String id) {
              this.id = id;
          }
      
          public String getType() {
              return type;
          }
      
          public void setType(String type) {
              this.type = type;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          public Image getImage() {
              return image;
          }
      
          public void setImage(Image image) {
              this.image = image;
          }
      
          public Image getThumbnail() {
              return thumbnail;
          }
      
          public void setThumbnail(Image thumbnail) {
              this.thumbnail = thumbnail;
          }
      }
      

      ViewMODEl 类

      public class ParsingVm extends BaseObservable {
      
          Context context;
      
          ApiInterface mApiInterface;
      
          public ParsingVm(Context context) {
      
              this.context = context;
              mApiInterface = AppController.getInstance().getmNetComponent().getApiInterface();
      
          }
      
          public Observable<NestedJson5Main> nestedJson5Main() {
      
              return AppController.getInstance().getmNetComponent().getApiInterface()
                      .nestedJson5()
                      .subscribeOn(Schedulers.newThread());
          }
          }
      

      ParsingMain

      public class ParsingMain extends AppCompatActivity {
      
      
          ActivityParsingBinding activityParsingBinding;
      
          ParsingVm parsingVm;
      
      
      
          @Override
          protected void onCreate(@Nullable Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              activityParsingBinding = DataBindingUtil.setContentView(this, R.layout.activity_parsing);
      
              parsingVm = new ParsingVm(this);
      
              activityParsingBinding.setParsingVm(parsingVm);
      
           receivenesteddjson5();
      
        }
      
        public void receivenesteddjson5() {
      
      
      
          parsingVm.nestedJson5Main()
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(nestedJson5Main -> {
      
                      Log.d(TAG, "receivenesteddjson5: " + nestedJson5Main.getImage().getHeight());
                      Log.d(TAG, "receivenesteddjson5: " + nestedJson5Main.getThumbnail().getUrl());
      
                  }, Throwable -> {
      
                      Throwable.printStackTrace();
                  });
      }
      
      
              }
      

      我添加了创建 Observable 然后 Observed 的简单答案。

      【讨论】:

      • 我已经用网络电话更新了答案。看看这个
      • @keerthiprasad 好的
      猜你喜欢
      • 2018-02-28
      • 1970-01-01
      • 2018-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多