是否真的有必要AndroidObservable.bindActivity(...)在下面的活动中使用?因为您仍然需要手动取消订阅onDestroy.
根据这篇博文,bindActivity是必要的,所以:
在有效的情况下,你不小心尝试改变状态
哪个 - 我读它的方式 - 意味着当你在finish()调用之后用活动做某些事情时会出现问题,因此在onDestroy中取消订阅会为时已晚.
任何人都可以给我一个例子,说明何时在onDestroy中取消订阅实际上是一个问题?
如果你看一下源为AndroidObservable.java用于bindActivity谓词函数为:
private static final Func1<Activity, Boolean> ACTIVITY_VALIDATOR = new Func1<Activity, Boolean>() {
@Override public Boolean call(Activity activity) {
return !activity.isFinishing();
}
};
Run Code Online (Sandbox Code Playgroud)
检查配置更改不是更好,例如:
private static final Func1<Activity, Boolean> ACTIVITY_VALIDATOR = new Func1<Activity, Boolean>() {
@Override public Boolean call(Activity activity) {
return !activity.isFinishing() && !activity.isChangingConfigurations();
}
};
Run Code Online (Sandbox Code Playgroud)
???
SomeActivity.java
public class SomeActivity extends Activity implements Observer<String> {
private Subscription subscription; …Run Code Online (Sandbox Code Playgroud) 我使用Retrofit与REST API交互,RxJava操作我收到的数据.
在下面的代码片段中,我进行了API调用,并使用map运算符保存我收到的数据,然后继续对流进行其他操作.
retrofitApi.registerDevice(mDevice)
.map(new Func1<Device, Device>() {
@Override
public Device call(Device device) {
// The only purpose of the map() operator
// is to save the received device.
magicBox.saveDeviceId(device.getId());
return device;
}
})
.otherOperations()...
Run Code Online (Sandbox Code Playgroud)
我的问题是:这个工作有更好的操作员吗?我觉得我误用了map操作员.
我有一个可以发出字符串的observable,我想用第一个字符对它们进行分组.这样做很容易groupBy:
Observable<String> rows = Observable.just("aa", "ab", "ac", "bb", "bc", "cc");
Observable<List<String>> groupedRows = rows.groupBy(new Func1<String, Character>() {
public Character call(String row) {
return row.charAt(0);
}
}).flatMap(new Func1<GroupedObservable<Character, String>, Observable<List<String>>>() {
public Observable<List<String>> call(GroupedObservable<Character, String> group) {
return group.toList();
}
});
groupedRows.toBlocking().forEach(new Action1<List<String>>() {
public void call(List<String> group) {
System.out.println(group);
}
});
// Output:
// [aa, ab, ac]
// [bb, bc]
// [cc]
Run Code Online (Sandbox Code Playgroud)
但它对我的目的并不好,因为groupBy只有在源可观察量发出时才完成每个组onComplete.因此,如果我有很多行,它们将完全聚集在内存中,并且只在最后一行"刷新"并写入输出.
我需要像buffer运算符这样的东西,但是我自己的函数表示每个组的边界.我实现了它(知道行总是按字母排序):
Observable<String> rows = Observable.just("aa", "ab", "ac", "bb", "bc", …Run Code Online (Sandbox Code Playgroud) 好的,所以我在我的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 != …Run Code Online (Sandbox Code Playgroud) 我正在尝试熟悉反应背压处理的问题,特别是通过阅读这个维基:https://github.com/ReactiveX/RxJava/wiki/Backpressure
在缓冲段落中,我们有更多涉及的示例代码:
// we have to multicast the original bursty Observable so we can use it
// both as our source and as the source for our buffer closing selector:
Observable<Integer> burstyMulticast = bursty.publish().refCount();
// burstyDebounced will be our buffer closing selector:
Observable<Integer> burstyDebounced = burstMulticast.debounce(10, TimeUnit.MILLISECONDS);
// and this, finally, is the Observable of buffers we're interested in:
Observable<List<Integer>> burstyBuffered = burstyMulticast.buffer(burstyDebounced);
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,我们通过为缓冲区运算符生成去抖动信号流来有效地去除突发源流.
但为什么我们需要在这里使用发布和引用计数器?如果我们放弃它们会导致什么问题?评论并没有让我更清楚,默认情况下RxJava Observables不是多播吗?
我正在使用一个名为AppWarp(http://appwarp.shephertz.com)的多人游戏客户端,您可以在其中添加事件监听器,以便在事件发生时回调,让我们假设我们将讨论连接侦听器,其中你需要实现这个接口:
public interface ConnectionRequestListener {
void onConnectDone(ConnectEvent var1);
void onDisconnectDone(ConnectEvent var1);
void onInitUDPDone(byte var1);
}
Run Code Online (Sandbox Code Playgroud)
我的目标是主要创建此客户端的Reactive版本,以便在我的应用程序内部使用,而不是直接使用客户端本身(我也将在以后依赖于接口,而不是像示例中那样依赖于WarpClient本身,但是这不重要,请在最后阅读我的问题).
所以我做的如下:
1)我引入了一个新事件,名为RxConnectionEvent(主要对Connection相关事件进行分组),如下所示:
public class RxConnectionEvent {
// This is the original connection event from the source client
private final ConnectEvent connectEvent;
// this is to identify if it was Connection / Disconnection
private final int eventType;
public RxConnectionEvent(ConnectEvent connectEvent, int eventType) {
this.connectEvent = connectEvent;
this.eventType = eventType;
}
public ConnectEvent getConnectEvent() {
return connectEvent;
}
public int getEventType() {
return eventType; …Run Code Online (Sandbox Code Playgroud) 作为开始使用RxAndroid的一个例子,我试图实现一个搜索框,当用户插入一些内容时触发一个休息调用.
到目前为止,我有两个工作部分.第一次观察EditTextView ......
RxTextView.textChangeEvents(searchEditText)
.debounce(400, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<TextViewTextChangeEvent>() {
@Override
public void onCompleted() {
Timber.d("onCompleted");
}
@Override
public void onError(Throwable e) {
Timber.e(e, "onError");
}
@Override
public void onNext(TextViewTextChangeEvent e) {
Timber.d("onNext" + e.text().toString());
}
});
Run Code Online (Sandbox Code Playgroud)
...以及使用Retrofit Service调用REST API的第二部分:
APIManager.getService().searchRestaurants("test")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<List<Restaurant>>() {
@Override
public void onCompleted() {
Timber.d("onCompleted");
}
@Override
public void onError(Throwable e) {
Timber.e(e, "onError");
}
@Override
public void onNext(List<Restaurant> restaurants) {
Timber.d("onNext");
for (Restaurant restaurant : restaurants) {
Timber.d(restaurant.getId() + ": " + …Run Code Online (Sandbox Code Playgroud) 我有我无法解决的问题。我试图使用Kotlin将.zip(List,)多个Singles合并为一个,而我提供的Function都不适合作为第二个参数。
fun getUserFriendsLocationsInBuckets(token: String) {
roomDatabase.userFriendsDao().getUserFriendsDtosForToken(token).subscribe(
{ userFriends: List<UserFriendDTO> ->
Single.zip(getLocationSingleForEveryUser(userFriends),
Function<Array<List<Location>>, List<Location>> { t: Array<List<Location>> -> listOf<Location>() })
},
{ error: Throwable -> }
)
}
private fun getLocationSingleForEveryUser(userFriends: List<UserFriendDTO>): List<Single<List<Location>>> =
userFriends.map { serverRepository.locationEndpoint.getBucketedUserLocationsInLast24H(it.userFriendId) }
Run Code Online (Sandbox Code Playgroud)
给定源可观察的S,我如何要求RxJava/Rx生成可观察的D,即:
大理石图:
我想用:
我更喜欢最简单的答案,即利用标准运算符(如果可能的话).
rx-java ×10
java ×5
android ×4
rx-android ×2
backpressure ×1
kotlin ×1
observable ×1
retrofit ×1
rx-kotlin ×1