如何在下面的函数中返回变量”a”。

huangapple 未分类评论45阅读模式
英文:

How to return the variable "a" in the function below

问题

以下是翻译好的部分:

这是一个简化的函数。

当我预期这个函数返回 5 时,它一直返回 0。为什么会这样呢?

public int accessKey() {
    a = 0;
    mSubscription = mAccountManager.getLoginPassword()
            .flatMap(loginPassword -> mServerAPI
                    .getProfilesList((new BaseRequest(
                            loginPassword.getLogin(),
                            loginPassword.getPassword(),
                            ClientGetter.getClientFromManager(),
                            CodeSnippets.getSha256(ClientGetter.getClientFromManager()))
                    )))
            .doOnNext(profilesListe -> mProfilesList = profilesListe.getItems())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new Subscriber<BaseResponse>() {
                @Override
                public void onCompleted() {
                    a = 5;
                }
                // ...
            });
    return a;
}
英文:

It's a simplified function.

It keeps returning 0 while I expect this to return 5. How come?

public int accessKey() {

    a = 0;

    mSubscription = mAccountManager.getLoginPassword()
            .flatMap(loginPassword -&gt; mServerAPI
    .getProfilesList((new BaseRequest(
            loginPassword.getLogin(),
            loginPassword.getPassword(),
            ClientGetter.getClientFromManager(),
            CodeSnippets.getSha256(ClientGetter.getClientFromManager()))
            )))
            .doOnNext(profilesListe -&gt; mProfilesList = profilesListe.getItems())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new Subscriber&lt;BaseResponse&gt;() {
                @Override
                public void onCompleted() {
                    a =5;

                   }
                      
    return a; }

答案1

得分: 0

因为异步或非阻塞行为。通常在API调用从数据库获取数据的情况下,我们会异步处理这些行为,否则您的应用程序会被阻塞。本文将帮助您解决这个问题。参考链接

英文:

It's because of the asynchronous or non blocking behaviour , Normally during the scenarios of API calls fetching some data from database ,these behaviours we handle asynchronously,because otherwise your application will get stuck This post will help you to solve this matter. Refer this

答案2

得分: 0

你在这里执行的是一个异步操作。你的代码在这里不是“自上而下”执行的,而是会在另一个线程上执行 - RxJava会将整个操作转移到另一个线程上,然后将结果返回到指定的线程,但这不会立即发生。在你的订阅代码执行时(我们不知道何时会发生),你的返回语句已经执行过了。

你可以尝试将你的代码更改为类似以下的形式(只是一个想法,我没有类似你的代码来创建一个可工作的示例):

return mAccountManager.getLoginPassword()
        .flatMap(loginPassword -> mServerAPI.getProfilesList(new BaseRequest(
                loginPassword.getLogin(),
                loginPassword.getPassword(),
                ClientGetter.getClientFromManager(),
                CodeSnippets.getSha256(ClientGetter.getClientFromManager())
        )))
        .doOnNext(profilesList -> mProfilesList = profilesList.getItems())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(Schedulers.io());

这意味着你的方法现在将返回一个可观察对象(Observable),你可以在其上订阅并在使用的地方订阅这些变化。

这也意味着你的方法的签名将不得不改变以支持这个新的返回类型,调用方法将需要订阅这个可观察对象。

你将你的问题描述为一个简化的函数,但我将为你提供一个甚至更简单的(完整)示例:

public Observable<Integer> foo() {
    return Observable.just(5);
}

public void usage() {
    Disposable disposable = foo()
        .subscribeOn(something)
        .observeOn(something)
        .subscribe(
            // 在这个subscribe内部,就像通常那样,你会得到结果5
        );
    // 处理 disposable
}
英文:

you're executing an asynchronous operation here. your code doesn't execute "top down" here but will be executed on a different thread - Rxjava shifts this entire operation over to another thread and then returns the result to the thread specified, but this doesn't happen immediately. by the time your subscribe code has executed (we don't know when that will be) your return statement has already executed.

you could try change your code to something like this (just as an idea, i don't have any code similar to yours to create a working example):

 return mAccountManager.getLoginPassword()
        .flatMap(loginPassword -&gt; mServerAPI
.getProfilesList((new BaseRequest(
        loginPassword.getLogin(),
        loginPassword.getPassword(),
        ClientGetter.getClientFromManager(),
        CodeSnippets.getSha256(ClientGetter.getClientFromManager()))
        )))
        .doOnNext(profilesListe -&gt; mProfilesList = profilesListe.getItems())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribeOn(Schedulers.io())

this means that your method will now return an observable, which you can then subscribe on and subscribe to those changes where they are used.

this means that the signature of your method will have to change to support this new return type and the calling method will have to subscribe to this observable.


you describe your question as a simplified function, but I'll give you an even simpler (complete) example:

public Observable&lt;Integer&gt; foo() {

    return Observable.just(5);

}

public void usage(){

  Disposable disposable = foo().subscribeOn(something).observeOn(something).subscribe(
   //inside this subscribe, like you&#39;d normally do, you&#39;d find the result of 5
  )
 ...
 //handle disposable
}

答案3

得分: 0

我使用了Callback接口的方法,它起作用了!非常感谢

英文:

I used the Callback interface method and it worked! Many thanks

huangapple
  • 本文由 发表于 2020年7月27日 16:34:38
  • 转载请务必保留本文链接:https://java.coder-hub.com/63111529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定