Rxjava窗口()令人困惑的结果

时间:2016-09-21 12:47:50

标签: rx-java

此刻我正在学习rxjava并陷入窗口操作员的困境。我正在我的Android设备上测试以下简单代码并得到令人困惑的结果。

代码:

Engine

有时候,我会得到适合我的结果:

> {
>     "user": {
>         "default_project_id": "d0f384973b9f4a57b975fcd9bef10c6e",
>         "description": "admin",
>         "domain_id": "default",
>         "email": "admin@gmail.com",
>         "enabled": true,
>         "id": "admin",
>         "links": {
>             "self": "http://localhost:5000/v3/users/admin1"
>         },
>         "name": "admin@gmail.com",
>         "username": "admin"
>     } }

但有时,我会得到像

这样的东西
2016-09-21 12:28:50.353 1483 WARNING keystone.common.wsgi [-] Authorization failed. The request you have made requires authentication. from 127.0.0.1
2016-09-21 12:28:50.445 1482 WARNING keystone.auth.controllers [-] User admin doesn't have access to default project d0f384973b9f4a57b975fcd9bef10c6e. The token will be unscoped rather than scoped to the project.
2016-09-21 12:28:50.767 1481 WARNING keystone.common.wsgi [-] You are not authorized to perform the requested action: identity:revoke_token
2016-09-21 12:29:42.900 1483 WARNING keystone.common.controller [-] RBAC: Bypassing authorization

有人可以解释为什么这段代码的结果会有所不同吗?

1 个答案:

答案 0 :(得分:0)

代码不会立即执行。从日志中可以看出,发出onNext事件的差异很小,因此它不等于100毫秒的多重性。如果添加窗口操作员间隙甚至更大。在此代码中,如果onNext事件遇到错误的窗口,则存在可能导致不需要的行为的边界情况

Observable.interval(100, TimeUnit.MILLISECONDS)
        .doOnNext(new Action1<Long>() {
            @Override
            public void call(Long aLong) {
                Log.d(TAG, "ON NEXT TIME    " + System.currentTimeMillis() % 10000);
            }
        })
        .take(10)
        .window(250, 100, TimeUnit.MILLISECONDS)
        .subscribe(new Action1<Object>() {
            @Override
            public void call(Object aLong) {
                Log.d(TAG, "SUBSCRIBE TIME: " + System.currentTimeMillis() % 10000);
            }
        });

没有窗口操作符的日志

SOME_TAG: ON NEXT TIME    8445
SOME_TAG: SUBSCRIBE TIME: 8445
SOME_TAG: ON NEXT TIME    8545
SOME_TAG: SUBSCRIBE TIME: 8545
SOME_TAG: ON NEXT TIME    8646
SOME_TAG: SUBSCRIBE TIME: 8646
SOME_TAG: ON NEXT TIME    8745
SOME_TAG: SUBSCRIBE TIME: 8745
SOME_TAG: ON NEXT TIME    8846
SOME_TAG: SUBSCRIBE TIME: 8846
SOME_TAG: ON NEXT TIME    8945
SOME_TAG: SUBSCRIBE TIME: 8945
SOME_TAG: ON NEXT TIME    9045
SOME_TAG: SUBSCRIBE TIME: 9045
SOME_TAG: ON NEXT TIME    9145
SOME_TAG: SUBSCRIBE TIME: 9146
SOME_TAG: ON NEXT TIME    9245
SOME_TAG: SUBSCRIBE TIME: 9245
SOME_TAG: ON NEXT TIME    9345
SOME_TAG: SUBSCRIBE TIME: 9345

使用窗口操作符

进行记录
SOME_TAG: ON NEXT TIME    7622
SOME_TAG: SUBSCRIBE TIME: 7718
SOME_TAG: ON NEXT TIME    7722
SOME_TAG: SUBSCRIBE TIME: 7818
SOME_TAG: ON NEXT TIME    7822
SOME_TAG: SUBSCRIBE TIME: 7918
SOME_TAG: ON NEXT TIME    7922 // THIS
SOME_TAG: SUBSCRIBE TIME: 8018
SOME_TAG: ON NEXT TIME    8023 // COMPARE WITH THIS
SOME_TAG: SUBSCRIBE TIME: 8118
SOME_TAG: ON NEXT TIME    8122
SOME_TAG: SUBSCRIBE TIME: 8218
SOME_TAG: ON NEXT TIME    8223
SOME_TAG: SUBSCRIBE TIME: 8319
SOME_TAG: ON NEXT TIME    8323
SOME_TAG: SUBSCRIBE TIME: 8419
SOME_TAG: ON NEXT TIME    8422
SOME_TAG: SUBSCRIBE TIME: 8518
SOME_TAG: ON NEXT TIME    8522