从lamda表达式返回

时间:2018-01-19 06:37:49

标签: android lambda java-8 viewmodel

在下面显示的代码中,我如何从这个observable(lambda表达式)返回布尔值

<?php
include('password.php');
class User extends Password{
    private $_db;
    function __construct($db){
        parent::__construct();
        $this->_db = $db;
    }
    private function get_user_hash($username){
        try {
            $stmt = $this->_db->prepare('SELECT password, username, memberID, investment,email FROM members WHERE username = :username AND active="Yes" ');
            $stmt->execute(array('username' => $username));
            return $stmt->fetch();
        } catch(PDOException $e) {
            echo '<p class="bg-danger">'.$e->getMessage().'</p>';
        }
    }
    public function isValidUsername($username){
        if (strlen($username) < 3) return false;
        if (strlen($username) > 17) return false;
        if (!ctype_alnum($username)) return false;
        return true;
    }
    public function login($username,$password){
        if (!$this->isValidUsername($username)) return false;
        if (strlen($password) < 3) return false;
        $row = $this->get_user_hash($username);
        if($this->password_verify($password,$row['password']) == 1){
            $_SESSION['loggedin'] = true;
            $_SESSION['username'] = $row['username'];
            $_SESSION['memberID'] = $row['memberID'];
            $_SESSION['email'] = $row['email'];
            $_SESSION['investment'] = $row['investment'];
            return true;
        }
    }
    public function logout(){
        session_destroy();
    }
    public function is_logged_in(){
        if(isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true){
            return true;
        }
    }
}
?>

这个语句块在一个返回布尔值的函数内,但是IDE告诉我案例中的意外返回语句。 谢谢您提前帮助

2 个答案:

答案 0 :(得分:0)

您试图从可观察函数内返回,而不是包含代码块的函数。这不起作用。假设checkEmailAndPassword上的调用是方法的重点,更好的选择是将callback函数传递给方法而不是返回布尔值。

答案 1 :(得分:0)

您缺少默认值:

loginActivityViewModel.checkEmailAndPassword(email,password).observe(this,(response)->{
    switch(response){

        case LoginActivityViewModel.EMPTY_EMAIL:
            handleError(emailWrapper, R.string.error_email_required);
            return false;

        case LoginActivityViewModel.INVALID_EMAIL:
            handleError(emailWrapper, R.string.error_enter_valid_email);
            return false;
        case LoginActivityViewModel.EMPTY_PASSWORD:
            handleError(passwordWrapper, R.string.error_password_required);
            return false;

        default:
            return false;

    }
});