findallAsync().asFlowable() 之后的运算符在 UI 线程上 运行
Operators after findallAsync().asFlowable() are running on the UI thread
我遇到了一个问题,rxjava 运算符 运行ning 在 UI 线程中。
我正在使用 findAllAsync() 异步获取对象并使用 asFlowable() 通过 rxjava 运算符处理它们。
realm.where(Specie.class)
.equalTo("fauna", true)
.findAllAsync().asFlowable()
.filter(new Predicate<RealmResults<Specie>>() {
@Override
public boolean test(RealmResults<Specie> species) throws Exception {
System.out.println("THREAD : " + Thread.currentThread().getId()); // Print 1
return species.isLoaded();
}
})
但是在 realm rxjava 示例中他们使用的是 observeOn(AndroidSchedulers.mainThread()) ,这意味着之前的运算符是异步的 运行ning 否则它将毫无用处。
Link : https://github.com/realm/realm-java/blob/master/examples/rxJavaExample/src/main/java/io/realm/examples/rxjava/animation/AnimationActivity.java
disposable = realm.where(Person.class).findAllAsync().asFlowable()
.flatMap(persons -> Flowable.fromIterable(persons))
.zipWith(Flowable.interval(1, TimeUnit.SECONDS), (person, tick) -> person)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(person -> {
TextView personView = new TextView(AnimationActivity.this);
personView.setText(person.getName());
container.addView(personView);
});
如何在 asFlowable() 之后异步地 运行 运算符?
edit : How can I access RealmResults obtained on the UI thread on a
background thread ?
在Schedulers.computation()
上执行,所以他们添加observeOn(AndroidSchedulers.mainThread())
回到主线程。
Realm 异步查询 API 自行处理查询的异步评估,并且该查询的结果由 Realm 传回 UI 线程,即 isLoaded()
是真的。
要离开主线程,可以使用observeOn(Schedulers.io())
作为例子。
我遇到了一个问题,rxjava 运算符 运行ning 在 UI 线程中。 我正在使用 findAllAsync() 异步获取对象并使用 asFlowable() 通过 rxjava 运算符处理它们。
realm.where(Specie.class)
.equalTo("fauna", true)
.findAllAsync().asFlowable()
.filter(new Predicate<RealmResults<Specie>>() {
@Override
public boolean test(RealmResults<Specie> species) throws Exception {
System.out.println("THREAD : " + Thread.currentThread().getId()); // Print 1
return species.isLoaded();
}
})
但是在 realm rxjava 示例中他们使用的是 observeOn(AndroidSchedulers.mainThread()) ,这意味着之前的运算符是异步的 运行ning 否则它将毫无用处。 Link : https://github.com/realm/realm-java/blob/master/examples/rxJavaExample/src/main/java/io/realm/examples/rxjava/animation/AnimationActivity.java
disposable = realm.where(Person.class).findAllAsync().asFlowable()
.flatMap(persons -> Flowable.fromIterable(persons))
.zipWith(Flowable.interval(1, TimeUnit.SECONDS), (person, tick) -> person)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(person -> {
TextView personView = new TextView(AnimationActivity.this);
personView.setText(person.getName());
container.addView(personView);
});
如何在 asFlowable() 之后异步地 运行 运算符?
edit : How can I access RealmResults obtained on the UI thread on a background thread ?
在Schedulers.computation()
上执行,所以他们添加observeOn(AndroidSchedulers.mainThread())
回到主线程。
Realm 异步查询 API 自行处理查询的异步评估,并且该查询的结果由 Realm 传回 UI 线程,即 isLoaded()
是真的。
要离开主线程,可以使用observeOn(Schedulers.io())
作为例子。