Hadi Wijaya
Hadi Wijaya

Reputation: 11

android parse, how to find other username?

ParseQuery<ParseUser> query = ParseUser.getQuery();
                query.whereEqualTo("username", "female");
                query.findInBackground(new FindCallback<ParseUser>() {
                    @Override
                    public void done(List<ParseUser> objects, com.parse.ParseException e) {
                        if (e == null) {
                            Toast.makeText(getApplicationContext(), "Username Found",
                                    Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "Username Not Found",
                                    Toast.LENGTH_LONG).show();
                        }
                    }
                });'

I have problem with these code, i want to search other username, please help me. I already write the code but always get in "Username found" but in database there no name like that.

Upvotes: 1

Views: 63

Answers (1)

user5462006
user5462006

Reputation:

You are wrong because, into findInBackground callback, you currently check only the e variable (if it is null or not) and not the objects size. Exception is alwasy null if the query return a valid result from Cloud Code (in this case the object is not null and it size is 0 or greather than); exception variable is valorized only if the execution of query raise an exception/error.

To check if a query returns a records or not you must to check the size of objects (List):

ParseQuery<ParseUser> query = ParseUser.getQuery();
            query.whereEqualTo("username", "female");
            query.findInBackground(new FindCallback<ParseUser>() {
                @Override
                public void done(List<ParseUser> objects, com.parse.ParseException e) {
                    if (e == null) {
                        if (objects.size() > 0) {
                            Toast.makeText(getApplicationContext(), "Username Found",
                                Toast.LENGTH_LONG).show();
                        }
                        else {
                            Toast.makeText(getApplicationContext(), "Username Not Found",
                                Toast.LENGTH_LONG).show();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "An error has occurred!",
                                Toast.LENGTH_LONG).show();
                    }
                }
            });

Take in consideration the opportunity to limit the query to 1 result (if you looking for one specific record) or use getFirstInBackground method to improve query performance.

Upvotes: 2

Related Questions