如何避免重复常见的可观察配置? [英] How can I avoid repeating common observable configuration?

查看:34
本文介绍了如何避免重复常见的可观察配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Retrofit 在 Android 中编写 API 客户端,这种代​​码被重复了很多次:

I'm writing an API client in Android using Retrofit and this sort of code gets repeated a lot:

myObservableFromRetrofit
  .subscribeOn(Schedulers.io())
  .observeOn(AndroidSchedulers.mainThread())
  .doOnError(... print stack trace ...)

我想知道是否有一种技术可以避免重复这些内容.

I'm wondering if there is a technique to avoid repeating this stuff.

我围绕对改造功能的调用:

I surrounding calls to retrofit functions with:

public Observable<?> commonObservable(Observable<?> observable) {
  return observable
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .doOnError(... print stack trace ...)
}

但这会丢失泛型类型信息.

But that loses generics type information.

推荐答案

Substitute ? for T 并添加缺失的 .这将允许 Type Inference 正确完成.

Substitute ? for T and add the missing <T>. That will allow for Type Inference to be done properly.

public <T> Observable<T> commonObservable(Observable<T> observable) {
  return observable
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .doOnError(... print stack trace ...)
}

这是通配符捕获的问题.

This is an issue with Wildcard capture.

在大多数情况下,您无需担心通配符捕获,除非您看到包含短语capture of"的错误消息.

For the most part, you don't need to worry about wildcard capture, except when you see an error message that contains the phrase "capture of".

我假设你指的是.这里有更多相关信息

Which I'm assuming you're refering to. Here's a bit more info about it

这篇关于如何避免重复常见的可观察配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆