Sort ParseQuery by boolean field - android

I want to sort my query first if a field is true or false, and then by updatedAt. How do I achieve this on Android with Parse SDK?
My current code is:
mQuery.orderByDescending("draft");
mQuery.addAscendingOrder("updatedAt");
But I get java.lang.IllegalArgumentException: Unable to sort by key draft.

First parse the query with draft values true with ascending order of updateTime and store the values to an array. After fetching this parse the query with draft values false.
ArrayList<ParseObject> result=new ArrayList();
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("draft", true);
query.orderByAscending("updatedAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
result=object;
ParseQuery<ParseObject> query1 = ParseQuery.getQuery("_User");
query1.whereEqualTo("draft", false);
query1.orderByAscending("updatedAt");
query1.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
result=object;
for(int i=0;i<object.size();i++)
{
result.add(object.get(i));
}
}
}
});
}
}
});

Try using
mquery.whereEqualTo("True","draft");
instead of orderByDescending.

Related

Query a ParseObject(Pointer) in User Class

Hello i am having some trouble in a Query. I made a pointer called "Empresa" in the _User class. i understand that this pointer is a ParseObject.So i did this i tried to do it in two ways...
private void queryEmpresa(){
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId);
query.include("Empresa");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject obj:objects
) {
empresa=obj.getParseObject("Empresa");
String id=empresa.getObjectId();
}
}
});
}
and also...
private void queryEmpresa(){
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("objectId", ParseUser.getCurrentUser().getObjectId());
query.include("Empresa");
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
for (ParseUser obj:objects
) {
empresa=obj.getParseObject("Empresa");
String id=empresa.getObjectId();
}
}
});
}
tell me which is the correct code and what do i need to modify in order to work. Can you please explain to me why is the reason this isnt working so that i dont incur into this problem in the near future?.
Try this and check if you get some error from the server:
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.include("Empresa");
query.getInBackground(ParseUser.getCurrentUser().getObjectId(), new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// object will be your user and you should be able to retrieve Empresa like this
empresa = object.getParseObject("Empresa");
} else {
// something went wrong. It would be good to log.
}
}
});

How to check if Username/Email already exists android parse

I've searched everywhere but haven't found anything about how to check if a username/email address in the parse database is taken. I've found some answers on the internet on parse.com forums but they weren't clear.
Thanks if you could help.
If this has an answer somewhere, then please comment instead of marking so I can delete it.
I think this will do what you need if I understand your question correctly:
final ParseQuery<ParseUser> emailQuery = ParseUser.getQuery();
emailQuery.whereEqualTo("email", emailAddress);
final ParseQuery<ParseUser> usernameQuery = ParseUser.getQuery();
usernameQuery.whereEqualTo("email", username);
List<ParseQuery> queries = new ArrayList<>();
queries.add(emailQuery);
queries.add(usernameQuery);
final ParseQuery<ParseUser> query = ParseQuery.or(queries);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> results, ParseException e) {
// results has the list of users that match either the email address or username
}
});
https://www.parse.com/docs/android/guide#queries-compound-queries
Or you could do it this way:
user.signUpInBackground(new SignUpCallback() {
public void done(ParseException e) {
if (e == null) {
// yippee!!
} else {
switch (e.getCode()) {
case ParseException.USERNAME_TAKEN: {
// report error
break;
}
case ParseException.EMAIL_TAKEN: {
// report error
break;
}
default: {
// Something else went wrong
}
}
}
}
});
Try this to check your data in parse. I hope this will helpful for you.
// this is create query of your parse table
ParseQuery<ParseObject> query = new ParseQuery<>(your prars tableName);
query.whereEqualTo(YourParseColumeNameEmail, yourEmail);
query.whereEqualTo(YourParseColumeNamePassword, yourPassword);
// this is for doing in background
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
if (scoreList.size() == 0) {
// if there is no data like your email and password then it,s come here
} else {
// if there is data like your email and password then it,s come here
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});

use specific value from ParseObject to a new query

I'm trying to get the value relatedGuild from ParseObject relationShipObject that has been sent to another method.
My code:
private void getRelation(){
Log.i("Status:", "Retrieving current user...");
//Retrieve the current logged in user
ParseUser currentUser = ParseUser.getCurrentUser();
Log.i("Status:", "Retrieving relationship...");
//Retrieve the relationship object for currentUser
ParseQuery<ParseObject> relationQuery = ParseQuery.getQuery("relation");
query.whereEqualTo("relatedUser", currentUser);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> relationShip, ParseException e) {
if (e == null) {
for (ParseObject relationShipObject : relationShip) {
// This does not require a network access.
relationshipObject.get("relatedGuild");
getGuild(relationShipObject);
}
} else {
Log.d("relation", "Error: " + e.getMessage());
}
}
});
}
private void getGuild(ParseObject relationShipObject){
Log.d("relation", "relationShipObject:" + relationShipObject.getString("relatedGuild"));
}
When i call Log.d in method getGuild i get a value equal to null. Am I trying to retrieve the value from row relatedGuild the wrong way? If yes, do you have any solution to the problem?
Update:
When i change from getString to get("relatedGuild").toString(), i get a value that looks like this: com.parse.ParseObject#21u702b7. That means relationShipObjectcontains some kind of value i don't know how to retrieve.
Try this:
private void getRelation(){
Log.i("Status:", "Retrieving current user...");
//Retrieve the current logged in user
ParseUser currentUser = ParseUser.getCurrentUser();
Log.i("Status:", "Retrieving relationship...");
//Retrieve the relationship object for currentUser
ParseQuery<ParseObject> relationQuery = ParseQuery.getQuery("relation");
query.whereEqualTo("relatedUser", currentUser);
query.include("relatedGuild"); // <-THIS INCLUDES THE OBJECT BEHIND THE POINTER
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> relationShip, ParseException e) {
if (e == null) {
for (ParseObject relationShipObject : relationShip) {
// This does not require a network access.
relationshipObject.get("relatedGuild");
getGuild(relationShipObject);
}
} else {
Log.d("relation", "Error: " + e.getMessage());
}
}
});
}
private void getGuild(ParseObject relationShipObject){
Log.d("relation", "relationShipObject:" + relationShipObject.getString("relatedGuild"));
}

how to fetch ParseUser object by objectId?

I have objectId and I want to fetch ParseUser object which has same objectId.
I have read that we can use get to get ParseUser by id at Parse documentation but syntax is not given there. If anyone know please help me.
I have tried this but it is not working.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("objectId", userId);
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> object, ParseException e) {
// TODO Auto-generated method stub
if(e==null)
for(ParseUser obj: object)
{
adress.setText(obj.getString("address"));
contact.setText(obj.getString("contact"));
}
else
{
adress.setText("Not found");
}
}
});
You can use the following code to get the user;
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("objectId",userId);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
//List contain object with specific user id.
} else {
// error
}
}
});
Hope this helps
Regards.

Add two users to parse object and Query

I want to create a parse object with two users and query for the object using both users. My get other user method returns the other user i want to add to the group.
String id;
final ParseUser[] user = new ParseUser[1];
public void getOtherUser()
{
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", "amanda");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
user[0] = objects.get(0);
addUserGroup();
} else {
// Something went wrong.
}
}
});
}
my add userGroup created a parse object with both users
private void addUserGroup()
{
final ParseObject group = new ParseObject("UserGroup");
group.put("from", ParseUser.getCurrentUser());
group.put("to", user[0]);
group.saveInBackground((new SaveCallback() {
public void done(ParseException e) {
id = group.getObjectId();
}
}));
}
After ive done this i want to be able to be able to update the parse object with new content and query for the object based on both users. The following mehtod is breaking on me though and im not sure why.
private void getData()
{
final double[] result = {0};
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserGroup");
query.whereEqualTo("from",ParseUser.getCurrentUser() );
query.whereEqualTo("to", user);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
Log.d("found", "woohoo");
}
});
Its returning the following error
java.lang.IllegalArgumentException: invalid type for ParseObject: class [Lcom.parse.ParseUser;
anybody know why this might be happening?
When you call ParseUser.getCurrentUser() parse return a user object. Now assuming you want to query usernames you need to change the ParseUser.getCurrentUser() to ParseUser.getCurrentUser().getUsername()
private void addUserGroup()
{
final ParseObject group = new ParseObject("UserGroup");
group.put("from", ParseUser.getCurrentUser().getUsername());
group.put("to", user[0]);
group.saveInBackground((new SaveCallback() {
public void done(ParseException e) {
id = group.getObjectId();
}
}));
}
private void getData()
{
final double[] result = {0};
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserGroup");
query.whereEqualTo("from",ParseUser.getCurrentUser().getUsername() );
query.whereEqualTo("to", user);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
Log.d("found", "woohoo");
}
});
}

Categories

Resources