Hi I have the next problem:
I have 10 users registered in Parse,a user can have the role of Admin or Player.
I'm trying to get all the users first but when I tried, the query only return one (The current user).
How can I get all the users? And then how can I filter by Role?
I have the next code:
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
if (e==null){
Log.i("OK","no problem");
}else{
Log.i("NOT OK","problem");
}
}
});
Honestly, for something as simple as this, it would be better for you not to use relations and separate classes at all and instead to simply add a string field to the User Class called "role." So go to your User class on Parse, add a new column of type String called "role" and then query it as follows.
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereEqualTo("role", "admin");
userQuery.findInBackground(new FindCallback<ParseUser>() {
void done(List<ParseUser> results, ParseException e) {
// results has the list of users who are admins
}
});
You should check the ACL(readAccess) of each user. The user is without read access right. The other 9 user may not with public read, not specify the user's id, and the role is not equip access right.
As A.Vin's answer, if your user have more than 1 role, you can use array of strings.
Related
I am building a social network application using parse which have follow/unfollow feature.
I have two tables: Users and Follow table.
Users table have user related data and Follow table have two fields: followerId and followedId.
I have a list of searched users. So, corresponding to each user i just want to get from query that whether i am following him/her or not. So, how to run query inside a loop?
Any help will be deeply appreciated.
Thanks.
For get follower
ParseQuery query = new ParseQuery("FollowTable");
query.whereEqualTo("followedId", ParseUser.getCurrentUser().getObjectId());
query.findInBackground(new FindCallback() {
public void done( List<ParseObject> MyList, ParseException e) {
if (e == null) {
// Get your follower user list
}else{
//error
}
For get following
ParseQuery query = new ParseQuery("FollowTable");
query.whereEqualTo("followerId", ParseUser.getCurrentUser().getObjectId());
query.findInBackground(new FindCallback() {
public void done( List<ParseObject> MyList, ParseException e) {
if (e == null) {
// Get your following user list
}else{
//error
}
Then after call user list query in ParseUser table and get all the user.
Thanks
I'm searching for a good way of querying usernames towards my app's user-database. So that people can't signup with the same username as someone else.
I'm using a username, password and an email for recover of password. So I want to check for username's basically.
I've understood this can be achieved with ParseQuery?
I would like an example if anyone can provide?
You can use a ParseQuery to query all usernames in the database like this.
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
// objects is a list of all the User you have. Just get all the usernames,
//and check if there's already one equal to the one you want to insert.
} else {
//error handling
}
}
}
I want to update a field from table 'User' of a ParseUser different by the one logged in.
The code below is not working and I was wondering if there is some impediment that does not allow to update other's user info.
ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.whereMatches("objectId", "4mdsJKKgDJ");
userQuery.getFirstInBackground(new GetCallback<ParseUser>() {
#Override
public void done(ParseUser parseUser, ParseException e) {
// I retreive the parseUser object correctly here
int score = parseUser.getInt("score"); // I have added this column in User table with a default value of 0
parseUser.put("score", score+1);
parseUser.saveInBackground(); // nothing happens. the value is not updated
}
});
Thanks.
I answer myself to my question: you cannot update another user info, different by the one logged in.
Using:
parseUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
System.out.println("ParseException: "+ e);
}
});
revealed the problem:
com.parse.ParseException: java.lang.IllegalArgumentException: Cannot save a ParseUser that is not authenticated.
So I will need to use another table to keep this info.
I have an app which works with users, and offers the possibility to follow certain users, at the current user choice.
Unfortunately, I don't know how to modify data of a user which is not the current user. I have not seen anything like that stated in the Parse.com docs(or i missed).
I have a column in my database in "Users" class called "usersFollowed" and when the current user clicks "Follow" i want to add the current user to the "usersFollowed" list, but adding them like you will see in the following code does not work.
Follow user code:
dialog = ProgressDialog.show(context, "",
"Following...", true);
viewHolder.userFollow.setSelected(true);
viewHolder.userFollow.setText("FOLLOWING");
ParseQuery<ParseUser> userListQuery = ParseUser.getQuery();
userListQuery.whereEqualTo("screenName", parseUserList.get(position).get("screenName").toString());
userListQuery.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> parseObjects, ParseException e) {
parseUserFollowedList = new ArrayList<>();
if (parseObjects.get(0).getList("usersFollowed") == null) {
parseUserFollowedList.add(ParseUser.getCurrentUser());
parseObjects.get(0).addAll("usersFollowed", parseUserFollowedList);
} else {
parseUserFollowedList = parseObjects.get(0).getList("usersFollowed");
parseUserFollowedList.add(ParseUser.getCurrentUser());
parseObjects.get(0).addAll("usersFollowed", parseUserFollowedList);
}
parseObjects.get(0).saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
dialog.dismiss();
}
});
}
});
.saveInBackground does not work because it throws exception: Cannot save info for user that is not logged in".
Can anyone help me in how to do this?
Cheers!
Users may modify only their own data. But the idea of following can be implemented such that only the currentUser's record requires write access: If Jack chooses to follow Jill, then Jack writes to his "following" relation. This works as long as we don't try to represent "followedBy" in Jill's data (which can be achieved instead with a query).
Or consider that the User table represents the private relationship between a real person and your app. It might make better sense to model the idea of a user's public face with your own custom object, and model following relationships between those. I mention this idea elsewhere here and here.
I have a registered app on Parse with a signup/login system. Now I want to create an activity which grabs all the available users in the app and to display them one-by-one as a listview. Can someone tell me how can I do that?
Once you've setup your ListView's adapter you can call this whenever you need to update it's contents:
public void updateUsers() {
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> userObjects, ParseException error) {
if (userObjects != null) {
mUserAdapter.clear();
for (int i = 0; i < userObjects.size(); i++) {
mUserAdapter.add(userObjects.get(i));
}
}
}
});
}
That updates my adapter which is plugged into my ListView. If you have more than 100 users you want to return you'll need to up the limit as I hear that's the default (then you have to page the results, not sure how yet)
Side note: As far as I know you can't Subclass the ParseUser at the moment (you can, but you then can't use that as the object you're querying with using ParseUser.getQuery() (Which would normally be done by ParseUser.getQuery(MyCustomUserClass.class) if you were to query a customised object.