简体   繁体   中英

Parse return value from query

I have simple query to "User" to my own column

                String myVar;
ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername().toString());
    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> objects, com.parse.ParseException e) {
            if (e == null) {
                ParseUser p = (ParseUser) objects.get(0);
                myVar = p.getString("Example");
            } else {
                //nothing to do
            }
        }
    });

My problem: I want to use my variable "myVal" in other parts of code, but it is always null, why? and how can I get value for this variable correctly?

Because parse executes and returns asynchronously. (think about loading a webpage; you can't read it before it loads).

If you want to be guaranteed a value for your variable, you have to put your code in the done method.

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