I have a table (ParseObject) called "Friends" with a "to" and "from" column which use pointers to objectId's of User's. This table tracks who's friends with who, how do I create a query that will return all my friends as user objects - not just the ID stored in that table?
Thanks!
The ParseUser class often confuses me as it is clearly different from a ParseObject and can't be used with the same methods (like ParseQuery.or)
Currently, I'm trying:
ParseQuery<ParseObject> friendsFrom = ParseQuery.getQuery("Friends");
friendsFrom.whereEqualTo("from", ParseUser.getCurrentUser());
ParseQuery<ParseObject> friendsTo = ParseQuery.getQuery("Friends");
friendsTo.whereEqualTo("to", ParseUser.getCurrentUser());
// where they've been accepted
friendsFrom.whereEqualTo("accepted", true);
friendsTo.whereEqualTo("accepted", true);
// combine the queries - parse does this a 'special' way
List<ParseQuery<ParseObject>> friends = new ArrayList<ParseQuery<ParseObject>>();
friends.add(friendsFrom);
friends.add(friendsTo);
ParseQuery<ParseObject> combinedQuery = ParseQuery.or(friends);
combinedQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> friends, ParseException e) {
if (friends != null) {
Log.d(getClass().getSimpleName(), "Found " + friends.size() + " friends");
contacts.clear();
for (ParseObject friend : friends) {
//show user, or whatever you'd like to do..
}
}
});
But a ParseObject cannot be cast to a ParseUser so I can't do anything here..
Update
According to Robert's advice I've tried the following, but it's still not providing me with the UserObjects I need - and for some reason, my Log aren't being written during these "findInBackground" callbacks by Parse..
ParseQuery < ParseObject > friendshipAndUserQuery = ParseQuery.getQuery("friends");
friendshipAndUserQuery.whereEqualTo("from", ParseUser.getCurrentUser().getObjectId());
friendshipAndUserQuery.include("to");
friendshipAndUserQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject friendship : objects) {
UserObject friend = (UserObject) friendship.getParseObject("to");
contacts.add(friend);
}
}
});
Related
I feel like I'm pretty close on this one, just need the last bit.
I have the following tables:
_User (standard Parse table)
Category (object Id, name)
Exercises (object Id, name, description, thumbnail, image, etc)
and UserFavourites which is where I store the user's preferred exercises
(objectId, user->users table, exercise->exercises table, category->category table)
I have writing to Parse using pointers just fine:
//create new parse object
ParseObject favouriteExercise = new ParseObject("UserFavourites");
//create pointers to the Exercise table and Category table
ParseObject exercise = ParseObject.createWithoutData("Exercises", mExerciseId);
ParseObject category = ParseObject.createWithoutData("Category", mCategoryId);
//put those pointers into the Userfavourites table and save
favouriteExercise.put("user",ParseUser.getCurrentUser());
favouriteExercise.put("exercise",exercise);
favouriteExercise.put("category",category);
//save
favouriteExercise.saveInBackground();
Now I'm trying to retrieve all the exercises a user has favourited and put them in to a listview by searching the table for any objects that match the user's pointer to the user's table:
ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
query.whereEqualTo("user", user);
//call to parse.com to start the query
query.findInBackground(new FindCallback<Exercises>() {
#Override
public void done(List<Exercises> exercises, ParseException e) {
if (exercises != null) {
Toast.makeText(getApplicationContext(), "Favourites found, can't list yet", Toast.LENGTH_SHORT).show();
mAdapter.clear();
//add all the exercises to the list
mAdapter.addAll(exercises);
//sort the list alphabetically
mAdapter.sort(new Comparator<Exercises>() {
#Override
public int compare(Exercises exercises, Exercises t1) {
return exercises.getName().compareTo(t1.getName());
}
});
} else {
mNoFavourites.setVisibility(View.VISIBLE);
}
Where I'm stuck is when I run this I can see my query is working -> I am retrieving the 4 rows in UserFavourites that I favourited out of the table of 8, so it is filtering correctly, but the objects I'm getting aren't pointing to the exercises I want. They are just empty pointers.
Thanks.
Yes it will return only reference (Pointer). If you want actual object data call fetchInBackground
myObject.fetchInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// Success!
} else {
// Failure!
}
}
});
I figured it out based on the logic kishore jethava gave.
I queried the favorites table, then with the results I wanted (which pointed to another table) I cycled through each result and got the object it pointed to and added it to my ArrayList.
public void getFavourites() {
//set progress bar
setProgressBarIndeterminateVisibility(true);
ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
query.whereEqualTo("user", user);
query.include("exercise");
//call to parse.com to start the query
query.findInBackground(new FindCallback<Exercises>() {
#Override
public void done(List<Exercises> objects, ParseException e) {
if (objects.size() != 0) {
for(ParseObject object : objects)
{
//for each pointer found, retrieve the object it points to
obj = object.getParseObject("exercise");
mAdapter.add((Exercises) obj);
}
});
}
} else {
mNoFavourites.setVisibility(View.VISIBLE);
}
//stop progress bar
setProgressBarIndeterminateVisibility(false);
}
});
}
Hello im trying to query a one to many relationship in parse. I have an Event table and invitation Table. An event has many invitations and many invitations are created by an event. The Event table has a column called status. Status has a value of 1 which means the owner has. In invitation table i have a column also called status and others users if going to an event will update the status to 1. I was successful in creating the table. Now i am trying to attempt to query both Event and Invitation table where the status is 1. But i am having trouble accomplishing this task. Below is my code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Event");
// Restrict to cases where the author is the current user.
//pass in a ParseUser and not String of that user
query.whereEqualTo("author", ParseUser.getCurrentUser());
query.whereEqualTo("Status", "1");
query.include("EventTitle");
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Invitation");
parseQuery.whereMatchesQuery("EventId", query);
// query.orderByAscending("createAt");
// Run the query
parseQuery.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objectList, ParseException e) {
if (e == null) {
// If there are results, update the list of event and notify the adapter
Log.d(TAG, "Im in background");
// eventList.clear();
invitationList.clear();
for (ParseObject event : objectList) {
invitationList.add((Invitation)event);
}
Log.d(TAG, String.valueOf(invitationList.size()));
updateEventsList();
} else {
Log.d(TAG, "Event retrieval error: " + e.getMessage());
}
}
});
}
And when i try to display in a ListView:
TextView tvTitle = (TextView)convertView.findViewById(R.id.textView_eventTitle);
Error: i get a null pointer indicating a fetch Might be needed. Please what i'm doing wrong
I believe you might have two options:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Event");
query.whereEqualTo("author", ParseUser.getCurrentUser());
query.whereEqualTo("Status", "1");
ParseQuery<ParseObject> parseQuery = ParseQuery.getQuery("Invitation");
parseQuery.whereEqualTo("EventId", id)
List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(query);
queries.add(parseQuery);
ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
mainQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> results, ParseException e) {
// results has the list of of invitations/events.
}
});
You can also try and save the eventId to the invitations table, that way you will not have to query two tables. That will however, duplicate some data, which you may or may not be okay with.
I've question about way in which I should create query to get expected data.
I've MyObject (ParseObject) which has pointer to ParseUser (column named createdByPtr). In ParseUser I have custom column userGroupPtr.
Now I want to get all MyObject rows which are created by all users (createdByPtr) belongs to user group (userGroupPtr) to which belongs current logged user (ParseUser.CurrentUser()).
At this moment I have some kind of draft like this:
ParseQuery<MyObject> query = ParseQuery.getQuery(MyObject.class);
query.include("createdByPtr");
query.whereEqualTo("createdByPtr.userGroupPtr", ParseUser.getCurrentUser().get("userGroupPtr"));
query.orderByDescending("updatedAt");
query.findInBackground(new FindCallback<MyObject>() {
#Override
public void done(List<MyObject> results, ParseException e) {
....
}
});
I don't have idea how should I transform line:
query.whereEqualTo("createdByPtr.userGroupPtr", ParseUser.getCurrentUser().get("userGroupPtr"));
in to working constrain.
Any suggestions?
Try like this. I have tested and it works perfectly. see Relational Queries
ParseQuery<ParseUser> innerQuery = ParseUser.getQuery();
innerQuery.whereEqualTo("userGroupPtr","userGroupPtr"); //value of userGroupPtr
ParseQuery<ParseObject> query = ParseQuery.getQuery(MyObject.class);
query.whereMatchesQuery("createdByPtr", innerQuery);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objectList, ParseException e) {
if(e == null){
}else{
Log.e("Object", e.getMessage());
}
}
});
I have a table of users in parse.com where each of them contains fields first_name and last_name. I'm doing user search and cannot figure out how to make condition OR.
My example:
ParseRelation<ParseUser> relation = user.getRelation("Friends");
ParseQuery<ParseUser> query = relation.getQuery();
query.whereContains("first_name", "typed text"); //need to add OR for the same condition,
//but for the field "last_name"
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
if(e == null){
//doing work
}else {
e.printStackTrace();
}
}
});
I tried to do this way:
query.whereContains("first_name", "typed text").whereContains("last_name", "typed text");
but no, it's searching for satisfying both conditions.
Method whereContainsAll() is applicable for list of values, but not keys.
I can do it by sending 2 requests, but this is horrible solution.
Is there a way we can do that?
You can use ParseQuery.or(queries)
Here is the docs link:
https://parse.com/docs/android/guide#queries-compound-queries
Updated 08-2022:
https://docs.parseplatform.org/android/guide/#compound-queries
Example code from doc:
ParseQuery<ParseObject> lotsOfWins = ParseQuery.getQuery("Player");
lotsOfWins.whereGreaterThan(150);
ParseQuery<ParseObject> fewWins = ParseQuery.getQuery("Player");
fewWins.whereLessThan(5);
List<ParseQuery<ParseObject>> queries = new ArrayList<ParseQuery<ParseObject>>();
queries.add(lotsOfWins);
queries.add(fewWins);
ParseQuery<ParseObject> mainQuery = ParseQuery.or(queries);
mainQuery.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> results, ParseException e) {
// results has the list of players that win a lot or haven't won much.
}
});
ParseQuery<ParseObject> query1 = new ParseQuery<ParseObject>("Place");
query1.whereEqualTo("email","xyz#gmail.com");
ParseQuery<ParseObject> query2 = new ParseQuery<ParseObject>("Place");
query2.whereEqualTo("place_mobile",909090XXXX);
List<ParseQuery<ParseObject>> list = new ArrayList<ParseQuery<ParseObject>>();
list.add(query1);
list.add(query2);
ParseQuery<ParseObject> query = ParseQuery.or(list);
query.findInBackground();
I know about how to insert in custom table in parse.com database but i don't know about it how to update. i confuse in the how to get all object value from the table. so you have any idea about it.
Thanks in Advances.
You could try to retrieve the object you want to update and then save it edited like this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("User");
query.whereEqualTo("firstName", "Dan");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> list, ParseException e) {
if (e == null) {
ParseObject person = list.get(0);
person.put("firstName", "Johan");
person.saveInBackground();
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
For more examples you could read the chapter for updating objects in Parse.com documentation here https://www.parse.com/docs/android/guide#objects-updating-objects