![](/img/trans.png)
[英]Android Architecture Components: How is LiveData in the repository observed by a ViewModel
[英]How to use RxJava instead of LiveData with android architecture components?
注意:如果您已经在使用RxJava或Agera之类的库,则可以继续使用它们代替LiveData。 但是,当您使用它们或其他方法时,请确保正确处理了生命周期,以使相关的LifecycleOwner停止时数据流暂停,而LifecycleOwner被破坏时数据流也被破坏。 您还可以添加android.arch.lifecycle:reactivestreams工件,以将LiveData与另一个反应流库(例如RxJava2)一起使用。
上面的语句是从android开发人员页面复制的,这里指定了是否使用RxJava,则不需要使用LiveData,因为两者均遵循Observable Patters。我想知道如何使用RxJava而不是通过rest api调用使用LiveData。很多,但是找不到任何答案。如果有人帮助我解决这个问题,那将是非常有意义的。 提前致谢
您可以使用这些方法将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
。
看着
https://developer.android.com/reference/android/arch/lifecycle/LiveDataReactiveStreams
RxJava的Flowable实现Publisher
接口。 您可以转换LiveData
到Flowable
并从那里走。
这是一个简单的示例,在该示例上,通过使用RX进行的改装来解析GET Respnse
假设回应
{
"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
}
ApiInterface
@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();
});
}
}
我已经添加了创建可观察对象然后被观察的简单答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.