在哪些情况下我应该使用doOnNext,在哪些情况下doOnEach?
.doOnEach(new Action1<Notification<? super MessageProfile>>() {
@Override
public void call(Notification<? super MessageProfile> notification) {
}
})
.doOnNext(new Action1<MessageProfile>() {
@Override
public void call(MessageProfile profile) {
messageProfileDao.save(profile);
}
})
Run Code Online (Sandbox Code Playgroud)
这看起来两个运算符具有相同的效果.
在我的应用中,我需要在后端处理应用内购买(而非订阅)的取消。在最好的情况下,我希望在用户取消应用程序时收到一些通知。
所以我找到了这个 API: https://developer.android.com/google/play/billing/realtime_developer_notifications 。不过这个东西好像只适合订阅。
这个 API 有意义:https : //developers.google.com/android-publisher/api-ref/purchases/voidedpurchases/list。我可以定期检查它,但随着用户数量的增加,这项任务可能会变得太耗时。
那么有没有更干净的方法来获取有关取消的应用程序内的通知?还是我必须一直检查状态?
我RecyclerView用LinearLayoutManager(Vertical不倒).当我将项目插入或移动到0位置时,会调用此方法:
private void scrollToStart() {
int firstVisiblePosition = ((LinearLayoutManager) recentList.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
boolean notScrolling = recentList.getScrollState() == RecyclerView.SCROLL_STATE_IDLE;
if (firstVisiblePosition < 2 && notScrolling) {
recentList.scrollToPosition(0);
}
}
Run Code Online (Sandbox Code Playgroud)
但有时它会平滑滚动到列表的末尾.
(我在Instagram等应用程序中看到过这样的行为.看起来列表滚动到顶部然后开始向相反的方向移动.)
如果我smoothScrollToPosition用simple 替换scrollToPosition它,它的行为应该如此.
如何防止此滚动?
我在构建期间遇到这样的错误:
e: /Users/some/path/SomeClass.java:86: error: cannot find symbol
e:
e: static ConnectionType getConnectionType(Context context) {
e: ^
e: symbol: class ConnectionType
e: location: class SomeClass
Run Code Online (Sandbox Code Playgroud)
ConnectionTypeprotobuf生成的类在哪里.所以看起来kapt不会解析生成的类.
我试过了什么?
起初我添加了kotlin-apt插件:
apply plugin: 'kotlin-kapt'
Run Code Online (Sandbox Code Playgroud)
然后我将brotobuf生成的类添加到源集:
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
main.java.srcDirs += 'build/generated/source/proto/main/java'
}
Run Code Online (Sandbox Code Playgroud)
而且我想在kapt开始工作之前生成类.所以我用这种方式命令gradle任务:
afterEvaluate {
def protoTasks = []
tasks.each { task ->
if (task.name.contains('proto') || task.name.contains('Proto')) {
protoTasks.push(task)
}
}
tasks.each { task ->
if (task.name.startsWith('kapt')) {
task.dependsOn protoTasks
}
}
}
Run Code Online (Sandbox Code Playgroud)
但所有这些事情都无济于事,我仍然有同样的错误.怎么解决?
在下面的代码中,我需要在unsubscription(它记录"release")上发布一些资源.
Observable first = Observable.create(new Observable.OnSubscribe<Object>() {
@Override
public void call(Subscriber<? super Object> subscriber) {
subscriber.add(Subscriptions.create(() -> {
log(“release”);
}));
}
}).doOnUnsubscribe(() -> log(“first”));
Observable second = Observable.create(…).doOnUnsubscribe(() -> log(“second”));
Observable result = first.mergeWith(second).doOnUnsubscribe(() -> log(“result”));
Subscription subscription = result.subscribe(…);
//…
subscription.unsubscribe();
Run Code Online (Sandbox Code Playgroud)
但它只记录"结果".看起来unsubscription没有传播到merge的子可观察量.那么如何处理第一个observable的Observable.OnSubscribe中的取消订阅?