简体   繁体   中英

int method returning wrong value

I'm sure my syntax is wrong somewhere... I have the following method:

private int getVersionStatus(){
        // Creates instance of the manager.
        AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(this);

        // Returns an intent object that you use to check for an update.
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();


        // Checks that the platform will allow the specified type of update.
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            versionCode=appUpdateInfo.updateAvailability();
            Log.d("ADebugTag", "Value: " + String.valueOf(appUpdateInfo.updateAvailability()));
            Log.d("ADebugTag", "Value123: " + String.valueOf(versionCode));     
        });

        return versionCode;        
    }

When I call the method from onCreate() both the logs print the correct value but the returned int (versionCode) is different...How is this so?

That is because versionCode is returned before addOnSuccessListener is called. The listener is set, getVersionCode() returns and eventually the lambda of addOnSuccessListener is called.

With the apis you have used now, returning a value is not possible. What you could do is pass a callback as an argument to getVersionCode() .

An example:

    interface Callback {
        void setVersion(int version);
    }

    private void getVersionStatus(Callback callback){
        // Creates instance of the manager.
        AppUpdateManager appUpdateManager = AppUpdateManagerFactory.create(this);

        // Returns an intent object that you use to check for an update.
        Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo();


        // Checks that the platform will allow the specified type of update.
        appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
            callback.setVersion(appUpdateInfo.updateAvailability());
        });
    }

    private void yourOtherFuncton() {
        getVersionStatus(version -> {
            Log.d("ADebugTag", "Value: " + String.valueOf(version));
        });
    }

It's basically them same as PPartisan' answer, with your own defined interface.

Based on your code, I'm assuming this is the section that retrieves your version code:

appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
    versionCode=appUpdateInfo.updateAvailability();
    //...
});

In which case, you're treating asynchronous code in a synchronous way. Without adding a dependency on another library (ie, RxJava), you can get around this by changing your method signature to use a callback instead of returning a value:

private void versionCode(IntConsumer consumer) {
    //...
    appUpdateInfoTask.addOnSuccessListener(appUpdateInfo -> {
        consumer.accept(appUpdateInfo.updateAvailability());
    });
}

With usage:

versionCode(code -> Log.d("TAG", "Version code is: " + code));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM