反应式编程与调用事件监听器(鼠标,键)中的函数有何不同,因为两者都是异步事件流,所以Reactive的优势是什么,而不是传统的事件监听器调用?
我有一份餐馆的预订清单.我希望在一年中将它们分组,并在当天按时间排序.我怎么能用rxjava这样做?
List reservations;
class Reservation {
public String guestName;
public long time; //time in milliseconds
}Run Code Online (Sandbox Code Playgroud)
OUTPUT
我有一个可供客户使用的API,可以简化为:
public class API {
public void sendEvent(Event e);
}
Run Code Online (Sandbox Code Playgroud)
Event每当客户端调用API(技术上将Binder转换为Service衍生产品)时,实例就会进入我的系统,然后处理,过滤并分派到其他内部组件.我不关心过去的事件,只关注用户订阅时可用的事件.对于Rx范例来说,这似乎是一种天生的选择,而我只是在沾沾自喜.
我需要一个创建一次的Observable,允许多个订阅者,并且可以Event通过反应管道向观察者发送其实例.一个Subject看起来适合什么,我希望做的(尤其是这个答案对这个问题和我产生共鸣).
其他RxJava用户推荐什么?
我们有一个应用程序,其中我们有一个物化的项目数组,我们将通过Reactive管道进行处理.它看起来有点像这样
EventLoopScheduler eventLoop = new EventLoopScheduler();
IScheduler concurrency = new TaskPoolScheduler(
new TaskFactory(
new LimitedConcurrencyLevelTaskScheduler(threadCount)));
IEnumerable<int> numbers = Enumerable.Range(1, itemCount);
// 1. transform on single thread
IConnectableObservable<byte[]> source =
numbers.Select(Transform).ToObservable(eventLoop).Publish();
// 2. naive parallelization, restricts parallelization to Work
// only; chunk up sequence into smaller sequences and process
// in parallel, merging results
IObservable<int> final = source.
Buffer(10).
Select(
batch =>
batch.
ToObservable(concurrency).
Buffer(10).
Select(
concurrentBatch =>
concurrentBatch.
Select(Work).
ToArray().
ToObservable(eventLoop)).
Merge()).
Merge();
final.Subscribe();
source.Connect();
Await(final).Wait();
Run Code Online (Sandbox Code Playgroud)
如果你真的很想玩这个,那么替身方法就像
private async static …Run Code Online (Sandbox Code Playgroud) 我正在使用RxJs来听amqp queu(不是真的相关).
我有一个函数createConnection返回一个Observable发出新连接对象的函数.一旦我有连接,我想每隔1000毫秒通过它发送消息,在10条消息之后我想要关闭连接.
我试图避免外部状态,但如果我不将连接存储在外部变量中,我该如何关闭它?请参阅我从连接开始,然后flatMap推送消息,因此在几个链之后我不再拥有连接对象.
这不是我的流程,但想象这样的事情:
createConnection()
.flatMap(connection => connection.createChannel())
.flatMap(channel => channel.send(message))
.do(console.log)
.subscribe(connection => connection.close()) <--- obviously connection isn't here
Run Code Online (Sandbox Code Playgroud)
现在我明白这样做是愚蠢的,但现在我如何访问连接?我当然可以先说var connection = createConnection()
然后以某种方式加入.但是我该怎么做?我甚至不知道如何正确地提出这个问题.Bottomline,我所拥有的是一个可观察的,发出连接,在连接打开后我想要一个每1000ms发出一次消息的observable(带a take(10)),然后关闭连接
我有一个重复元素的列表,说:
Observable<String> source = Observable.just("A", "B", "A", "C", "C", "A");
Run Code Online (Sandbox Code Playgroud)
我想根据它们的值以及它们出现的次数对它们进行分组,因此输出将是以下对:
{"A", 3}, {"B", 1}, {"C", 2}
Run Code Online (Sandbox Code Playgroud)
基本上相当于一个SQL语句 SELECT x, COUNT(1) GROUP BY x;
我到目前为止只打电话给groupBy:
source.groupBy(x -> x, x -> 1)
Run Code Online (Sandbox Code Playgroud)
但是这会将流转换为GroupedObservables,我找不到如何继续使用它们的好例子.我试过了reduce(),但这里并不好,因为groupBy()它想要减少GroupedObservables,而不是每个组内的元素.
这可能与GroupedObservables有关吗?有没有其他方法可以达到预期的效果?
我有一个3平行的firebase需要.我需要将它们作为单个对象加入.代码如下:
import {Http} from "@angular/http";
import {AngularFireDatabase} from "angularfire2";
import {Thread} from "../model/thread";
import {Message} from "../model/message";
import {Participant} from "../model/participant";
constructor(private http: Http, private db:AngularFireDatabase) { }
loadUserThreads(uid): Observable<AllUserData> {
return Observable.forkJoin([
this.db.list('participant/' + uid + '/threads').first(),
this.db.list('participant/' + uid + '/messages').first(),
this.db.list('participant/' + uid + '/participants').first()
])
.map((data:any[]) => {
let threads: Thread[] = data[0];
let messages: Message[] = data[1];
let participants: Participant[] = data[2];
let AllUserData = {
threads: threads,
messages: messages,
participants: participants
}
return AllUserData; …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个轮询网络服务的流.此时它会查询服务,然后在短暂延迟后完成.我希望继续流重启而不是完成从而永远轮询服务.
你可以做点什么......
myPollingStream.repeat()
Run Code Online (Sandbox Code Playgroud)
但repeat实际上在RxSwift中实际上repeatElement会产生一个可观察的流.您可能将concatMap这些变为扁平的序列序列,但RxSwift没有concatMap运算符.
那么如何在RxSwift中循环一个observable呢?
我希望请求是顺序的,而不是并发的,所以flatMap不是一个选项,因为它合并了导致重叠请求的流.我正在寻找类似于retry()工作的东西但onComplete不重新开始onError
如果客户端断开连接(false)超过10秒钟,我想显示一个弹出窗口。但是,当连接重新获得时,我也将关闭弹出窗口(true)。如果需要,我需要立即将弹出窗口关闭。
我认为我需要做的是根据值(false)进行反跳,但我不确定。
mConnectionObservable
.distinctUntilChanged()
.debounce(10, TimeUnit.SECONDS)
.subscribe(online -> {
if (online) {
//Dismiss popup
} else {
//Show popup about internet connection
}
});
Run Code Online (Sandbox Code Playgroud) 我想从Observable获取字符串值并将函数中的值返回给调用函数.
例如:我有一系列的键,并希望逐个获取所有键的值(字符串),并显示具有菜单栏的html组件.
这是ts文件:
public data = [ { 'key': 1, 'value' : this.getValue(1)},
{ 'key': 2, 'value' : this.getValue(2)},
...
];
private getValue(key: number): string {
return this.keyService.find(key).subscribe(response => {
return response;
});
}
Run Code Online (Sandbox Code Playgroud)
...
public find(key:number): Observable<any> {
return this.http.get(`/api/keys/${key}`).map(res => res.json() || {});
}
...
Run Code Online (Sandbox Code Playgroud)
我想显示html组件中的所有值.但是在key-list.component.ts中获取可观察到类型字符串的错误.
我如何解决这个订阅方法,并确保getValue应该始终返回字符串,并使其完美无瑕.
其中一个解决方案是:
private getValue(key: number): string {
let result: string;
this.keyService.find(key).subscribe(res => result = res);
return result;
}
Run Code Online (Sandbox Code Playgroud)
上述解决方案并不总是有效.这类问题的替代解决方案是什么?
rx-java ×4
rxjs ×3
java ×2
rx-android ×2
.net ×1
android ×1
angular ×1
c# ×1
firebase ×1
javascript ×1
node.js ×1
observable ×1
rx-swift ×1
swift ×1
typescript ×1